java I/O流,输入流,输出流,输入输出流,InputStream,FileInputStream,文件流,byte,url
来源:互联网 发布:沼泽人思想实验知乎 编辑:程序博客网 时间:2024/06/05 09:23
//把F:/test/distribute.sql这个文件读入到byte数组中,返回这个数组
package com.cus.systemFunction.disCom;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import org.apache.log4j.Logger;
/**
* 说明:把文件读到byte数组
* @author xxx
* 创建时间:2010-8-5 下午01:21:33
*/
public class FileToByteArray {
static Logger logger = Logger.getLogger(FileToByteArray.class.getName());
String outDir = "";
//getByteArray() 方法返回一个byte数组
public byte[] getByteArray() throws Exception {
// String path= getClass().getResource("/").getPath();
// outDir = path.trim().substring(1,path.length()-5)+"outputDir/generator/sqlScript/";
//BufferedInputStream in = new BufferedInputStream(new FileInputStream(outDir+"distribute.sql"));
BufferedInputStream in = new BufferedInputStream(new FileInputStream("F://test//distribute.sql"));
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
System.out.println("Available bytes:" + in.available());
byte[] temp = new byte[1024];
int size = 0;
while ((size = in.read(temp)) != -1) {
out.write(temp, 0, size);
}
in.close();
byte[] content = out.toByteArray();
System.out.println("Readed bytes count:" + content.length);
return content;
}
}
//把上面返回的这个byte数组的内容,读入到C盘目录下的distributeCopy.sql文件中
package com.cus.systemFunction.disCom;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
public class TestMain {
public static void main(String args[]) throws Exception{
FileToByteArray fileToByteArray = new FileToByteArray();
byte[] buffer = fileToByteArray.getByteArray();
FileOutputStream outf=new FileOutputStream("C://distributeCopy.sql");
BufferedOutputStream bufferout= new BufferedOutputStream(outf);
bufferout.write(buffer);
bufferout.flush();
bufferout.close();
}
}
/**
* 读文件,根据文件名,返回文件内容字符串
*/
public String readFile(String filePath){
StringBuffer sb = new StringBuffer();
try {
String encoding = "UTF-8";
File file = new File(filePath);
//判断文件是否存在
if(file.isFile() && file.exists()){
InputStreamReader read = new InputStreamReader(new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){
// System.out.println(lineTxt);
sb.append(lineTxt).append("\r\n");
}
read.close();
}else{
System.out.println(sb.append("找不到指定的文件:" + filePath));
}
} catch (Exception e) {
System.out.println(sb.append("读取文件内容出错"));
e.printStackTrace();
}
return sb.toString();
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//文件拷贝,把一个文件读入InputStream,然后把InputStream写入另一个文件
public class Test2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
File file = new File("D:\\sampleanalysisreport\\28.docx");
try {
InputStream inputStream = new FileInputStream(file);
try {
saveToFile(inputStream,
"D:\\sampleanalysisreport_sub\\28_bak3.docx");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//把InputStream写入文件
public static void saveToFile(InputStream inputStream, String fileName)
throws IOException {
File file = new File(fileName);
OutputStream os = null;
try {
os = new FileOutputStream(file);
byte buffer[] = new byte[4 * 1024];
int len = 0;
while((len = inputStream.read(buffer)) != -1){
os.write(buffer,0,len);
}
os.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
-------------------------------------------------------------------------------
/**
* 把指定url下载的文件保存下来
* @param destUrl
* @param fileName
* @return
*/
public static boolean saveToFile(String destUrl, String fileName) {
boolean result = false;
FileOutputStream fos = null;
BufferedInputStream bis = null;
HttpURLConnection httpUrl = null;
URL url = null;
byte[] buf = new byte[1024];
int size = 0;
try {
url = new URL(destUrl);
httpUrl = (HttpURLConnection) url.openConnection();
httpUrl.connect();
bis = new BufferedInputStream(httpUrl.getInputStream());
fos = new FileOutputStream(fileName);
while ((size = bis.read(buf)) != -1)
fos.write(buf, 0, size);
fos.close();
bis.close();
System.out.println("saveToFile---------successful");
result = true;
} catch (IOException e) {
// TODO: handle exception
System.out.println("saveToFile---------failure");
e.printStackTrace();
}finally{
httpUrl.disconnect();
}
return result;
}
---------------------------------------------------------------------------------------------------
String与InputStream相互转换
1.String to InputStream
String str = "String与InputStream相互转换";
InputStream in_nocode = new ByteArrayInputStream(str.getBytes());
InputStream in_withcode = new ByteArrayInputStream(str.getBytes("UTF-8"));
2.InputStream to String
这里提供几个方法。
方法1:
public String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "/n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
方法2:
public String inputStream2String (InputStream in) throws IOException {
StringBuffer out = new StringBuffer();
byte[] b = new byte[4096];
for (int n; (n = in.read(b)) != -1;) {
out.append(new String(b, 0, n));
}
return out.toString();
}
方法3:
public static String inputStream2String(InputStream is) throws IOException{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i=-1;
while((i=is.read())!=-1){
baos.write(i);
}
return baos.toString();
}
---------------------------------------------------------------------------------------------------
- java I/O流,输入流,输出流,输入输出流,InputStream,FileInputStream,文件流,byte,url
- I/O流之进步认识、InputStream以及FileInputStream
- JavaSE I/O 输入字节流 InputStream
- Java I/O流-FileInputStream、FileOutputStream
- 【JAVA】文件输入流 FileInputStream
- java输入与输出(I/O)(二):常见输入输出流小结
- Java I/O 输入输出流
- Java I/O输入输出流
- Java I\O输入输出流
- Java I/O输入输出流
- Java I/O输入输出流
- java I/O 输入输出流
- FileInputStream(文件输入、输出流) 示例
- Java文件输入/输出流 FileOutputStream和FileInputStream
- FileInputStream--文件输入流
- I/O-输入输出流
- 输入输出流 I/O
- I/O输入输出流
- C# 调用DLL
- 深入理解Aireplay-ng各种攻击及其原理
- Spring中操作applicationContext.xml
- WPF中bitmapimage与byte[]格式相互转换
- 没有实践,当然没有机会
- java I/O流,输入流,输出流,输入输出流,InputStream,FileInputStream,文件流,byte,url
- Qt 类型编辑实例-指定QPushButton 菜单指示器的子控制
- winpe下硬盘不存在,自动安装恢复ghost不存在
- 软件实施顾问的基本技能和需求控制
- 三角函数
- 列表ul的基本用法分析
- 好产品的简洁之道
- C实现html页面的跳转
- FULL HD