Java读写⽂件⽅法总结
Java读写⽂件⽅法总结
Java的读写⽂件⽅法在⼯作中相信有很多的⽤处的,本⼈在之前包括现在都在使⽤ Java的读写⽂件⽅法来处理数据⽅⾯的输⼊输出,确实很⽅便。奈何我的记性实在是叫⼈着急,很多时候既然都会想不起来怎么写了,不过我的Java 代码量也实在是少的可怜,所以应该多多练习。这⾥做⼀个总结,集中在⼀起⽅⾯今后查看。
Java读⽂件
1 package天才⽩痴梦;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.FileReader;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.InputStreamReader;
10 import java.io.RandomAccessFile;
11 import java.io.Reader;
12
13 public class JavaIO {
14
15 /**java stream
16 * 采⽤的是操作系统底层默认的编码⽅式,GBK等,⾮UTF8
17 * */
18
19 /**
20 * 以字节为单位读取⽂件内容,常⽤于读取⼆进制⽂件,⽐如图⽚、影像、声⾳等⽂件
21 * */
22 public static void readFileByBytes(String filename) {
23 File file=new File(filename);
24 FileInputStream in=null;
25 try {
26 System.out.println("以字节为单位读取⽂件,⼀次读⼀个字节: ");
27 in=new FileInputStream(file);
28 int temp=0;
29 while ((ad()) != -1) {
30 System.out.println(temp);
31 }
32 in.close();
33 } catch (IOException e) {
34 e.printStackTrace();
35 return ;
36 }
37 try {
38 System.out.println("以字节为单位读取⽂件,⼀次读多个字节: ");
39 byte[] temp=new byte[100];
40 int byteread=0;
41 in=new FileInputStream(file);
42 JavaIO.showAvailableBytes(in);
43 while ((ad(temp)) != -1) {
44 System.out.write(temp,0,byteread);
45 }
46 } catch (Exception e1) {
47 e1.printStackTrace();
48 } finally {
49 if (in != null) {
50 try {
51 in.close();
52 } catch (IOException e1) {
53
54 }
55 }
56 }
57 }
58 /**
59 * 以字符为单位读取⽂件,常⽤于读⽂本,数字等类型的⽂件
60 * */
61 public static void readFileByChar(String filename) {
62 File file=new File(filename);
63 Reader reader=null;
64 try {
65 System.out.println("以字符为单位读取⽂件内容,⼀次⼀个字节:");
66 //InputStreamReader类:是字节向字符转换的桥梁
67 reader=new InputStreamReader(new FileInputStream(file));
68 int temp;
69 while ((ad()) != -1) {
70 if (((char)temp) != '\r') {
71 System.out.println((char)temp);
72 }
73 }
74 reader.close();
75 } catch (Exception e) {
76 e.printStackTrace();
77 }
78 try {
79 System.out.println("以字符为单位读取⽂件内容,⼀次读多个字节: ");
80 char[] temp=new char[30];
81 int charread=0;
82 reader=new InputStreamReader(new FileInputStream(filename));
83 while ((ad(temp)) != -1) {
84 if ((charread == temp.length) && (temp[temp.length-1]!='\r')) {
85 System.out.println(temp);
86 } else {
87 for (int i=0; i<charread; i++) {
88 if (temp[i] == '\r') {
89 break;
90 } else {
91 System.out.println(temp[i]);
92 }
93 }
94 }
95 }
96 } catch (Exception e) {
97 e.printStackTrace();
98 } finally {
99 if (reader != null) {
100 try {
101 reader.close();
102 } catch (IOException e) {
103
104 }
105 }
106 }
107 }
108 /**
109 * 以⾏为单位读取⽂件,常⽤于读⾯向⾏的格式化⽂件
110 * */
111 public static void readFileByLine(String filename) {
112 File file=new File(filename);
113 BufferedReader reader=null;
114 try {
115 System.out.println("以⾏为单位读取⽂件内容,⼀次读⼀整⾏: "); 116 reader=new BufferedReader(new FileReader(file));
117 String temp=null;
118 int line=1;
119 while ((adLine()) != null) {
120 System.out.println("line " + line + ": " + temp);
121 line++;
122 }
123 reader.close();
124 } catch (IOException e) {
125 e.printStackTrace();
126 } finally {
127 if (reader != null) {
128 try {
129 reader.close();
130 } catch (IOException e) {
131
132 }
133 }
134 }
135 }
136 /**
137 * 随机读取⽂件内容
138 * */
139 public static void readFileByRandomAccess(String filename) {
140 RandomAccessFile randomfile=null;
141 try {
142 System.out.println("随机读取⼀段⽂件内容");
143 randomfile=new RandomAccessFile(filename,"r");
144 long fileLength=randomfile.length();
145 int beginIndex=(fileLength > 4 ? 4 : 0);
146 randomfile.seek(beginIndex);
147 byte[] bytes=new byte[10];
148 int byteread=0;
149 while ((ad(bytes)) != -1) {
150 System.out.write(bytes,0,byteread);
151 }
152 } catch (IOException e) {
153 e.printStackTrace();
154 } finally {
155 if (randomfile != null) {
156 try {
157 randomfile.close();
158 } catch (IOException e) {
159
160 }
161 }
162 }
163 }
164 private static void showAvailableBytes(InputStream in) {
165 try {
166 System.out.println("当前字节输⼊流中的字节数为:" + in.available());
167 } catch (IOException e) {
168 e.printStackTrace();
169 }
170 }
171 public static void main(String[] args) {
172 String filename="E:\\";
173 adFileByBytes(filename);
174 adFileByChar(filename);
175 adFileByLine(filename);
176 adFileByRandomAccess(filename);
177 }
178 }
Java写⽂件
1 package天才⽩痴梦;
2
3 import java.io.BufferedWriter;
4 import java.io.File;
5 import java.io.FileNotFoundException;
6 import java.io.FileOutputStream;
7 import java.io.FileWriter;
8 import java.io.IOException;
9 import java.io.OutputStreamWriter;
10
11 public class JavaIO2 {
12
13 public static void main(String[] args) throws IOException {
14 String Path="E:\\天才⽩痴梦\\JAVA";
15 File file=new File("E:\\天才⽩痴梦\\JAVA","");
16 if (!ists()) {
17 try {
18 ateNewFile();
19 } catch (IOException e) {
20 e.printStackTrace();
21 }
22 }
23 /**
24 * Java写⼊⽂件的三种⽅法
25 * */
26 FileOutputStream fos=null;
27 BufferedWriter bw=null;
28 FileWriter fw=null;
29 int value=1000;
30
31 try {
32 fos=new FileOutputStream(new File(Path+""));
33 long begin=System.currentTimeMillis();
34 for (int i=1; i<=value; i++) {
35 fos.write(5);
36 }
37 long end=System.currentTimeMillis();
38 System.out.println("TheCostTime of FileOutputStream is : " + (end-begin));
39 fos.close();
40
41 bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(Path+"br.txt")),"UTF8"));
42 begin=System.currentTimeMillis();
43 for (int i=1; i<=value; i++) {
44 bw.write(5);
45 bw.newLine();
46 }
47 bw.close();
48 end=System.currentTimeMillis();
49 System.out.println("TheCostTime of BufferedWriter is : " + (end-begin));
50
51 fw=new FileWriter(Path+"fw.txt");
52 begin=System.currentTimeMillis();
53 for (int i=1; i<=value; i++) {
54 fw.write(5);
55 }
56 fw.close();
57 end=System.currentTimeMillis();
58 System.out.println("TheCostTime of FileWriter is : " + (end-begin));
59
60
61 } catch (Exception e) {
62 // TODO Auto-generated catch block
63 e.printStackTrace();
64 } finally {
65 try {
66 fos.close(); //FileOutputStream
67 bw.close(); //BufferedWriter
68 fw.close(); //FileWriter
69 } catch (Exception e) {
70 e.printStackTrace();
71 }
72 }
73
74 }
75 }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论