1 importjava.io.BufferedOutputStream;2 importjava.io.BufferedWriter;3 importjava.io.ByteArrayOutputStream;4 importjava.io.File;5 importjava.io.FileInputStream;6 importjava.io.FileOutputStream;7 importjava.io.FileWriter;8 importjava.io.IOException;9 importjava.io.OutputStreamWriter;10
11 public classFileHelper {12 //第一种获取文件内容方式
13 public byte[] getContent(String filePath) throwsIOException {14 File file = newFile(filePath);15
16 long fileSize =file.length();17 if (fileSize >Integer.MAX_VALUE) {18 System.out.println(“file too big…”);19 return null;20 }21
22 FileInputStream fi = newFileInputStream(file);23
24 byte[] buffer = new byte[(int) fileSize];25
26 int offset = 0;27
28 int numRead = 0;29
30 while (offset
32 && (numRead = fi.read(buffer, offset, buffer.length – offset)) >= 0) {33
34 offset +=numRead;35
36 }37
38 //确保所有数据均被读取
39
40 if (offset !=buffer.length) {41
42 throw new IOException(“Could not completely read file “+file.getName());43
44 }45
46 fi.close();47
48 returnbuffer;49 }50
51 //第二种获取文件内容方式
52 public byte[] getContent2(String filePath) throwsIOException53 {54 FileInputStream in=newFileInputStream(filePath);55
56 ByteArrayOutputStream out=new ByteArrayOutputStream(1024);57
58 System.out.println(“bytes available:”+in.available());59
60 byte[] temp=new byte[1024];61
62 int size=0;63
64 while((size=in.read(temp))!=-1)65 {66 out.write(temp,0,size);67 }68
69 in.close();70
71 byte[] bytes=out.toByteArray();72 System.out.println(“bytes size got is:”+bytes.length);73
74 returnbytes;75 }76 //将byte数组写入文件
77 public void createFile(String path, byte[] content) throwsIOException {78
79 FileOutputStream fos = newFileOutputStream(path);80
81 fos.write(content);82 fos.close();83 }84 /*方法1:85 * 将byte数组(追加)写入文件86 *87 **/
88 public void createFileAdd(String path, byte[] content, boolean Appendable) throwsIOException {89 //程序写好之后每次存储数据都刷新90 //FileOutputStream fos = new FileOutputStream(path);91 //研究了一下,原来FileOutPutStream也可以设置模式的,只是和openFileOutput不一样 我这样写FileOutputStream fos=new FileOutputStream(_sdpath1,Appendable);就可以实现数据追加功能
92 FileOutputStream fos=newFileOutputStream(path,Appendable);93 fos.write(content);94 fos.write(“\r\n”.getBytes());95 fos.close();96 }97 /**
98 * 方法二:99 * 根据byte数组,生成文件100 */
101 public void writeFile(byte[] bfile, String filePath,String fileName) {102 BufferedOutputStream bos = null;103
104 File file = null;105 try{106 File dir = newFile(filePath);107 if(!dir.exists()&&dir.isDirectory()){//判断文件目录是否存在
108 dir.mkdirs();109 }110 file = new File(filePath+”\\”+fileName);111 /*使用以下2行代码时,不追加方式*/
112 /*bos = new BufferedOutputStream(new FileOutputStream(file));113 bos.write(bfile);*/
114
115 /*使用以下3行代码时,追加方式*/
116 bos = new BufferedOutputStream(new FileOutputStream(file, true));117 bos.write(bfile);118 bos.write(“\r\n”.getBytes());119
120
121 bos.flush();122
123 } catch(Exception e) {124 e.printStackTrace();125 } finally{126 if (bos != null) {127 try{128 bos.close();129 } catch(IOException e1) {130 e1.printStackTrace();131 }132 }133
134 }135 }136 }