java读取文件
1 java8读取文本文件
2
3
4 public static void java8ReadFileLines(String fileName) throws IOException {
5 List lineList = Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8);
6
7 for(String line:lineList){
8 System.out.println(line);
9 }
10 }
11
12
13
14 一行一行地读取文件
15
16
17 public static void readFileByLines(String fileName) {
18 File file = new File(fileName);
19 BufferedReader reader = null;
20 try {
21 reader = new BufferedReader(new FileReader(file));
22 String line = null;
23 while((line=reader.readLine())!=null ) {
24 System.out.println(line);
25 }
26 }catch (IOException e) {
27
28 }finally {
29 if(reader!=null) {
30 try{
31 reader.close();
32 }catch (IOException e) {
33 ;
34 }
35 }
36 }
37 }
38
39
40
41 一次读取多个字符
42
43
44 public static void readFileByMultiChars(String fileName) {
45 File file = new File(fileName);
46 try{
47 InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file));
48
49 char[] tempChars = new char[30];
50
51 int readCount = 0;
52 while((readCount=inputStreamReader.read(tempChars))!=-1) {
53 if(readCount==tempChars.length) {
54 System.out.println(tempChars);
55 }else{
56 System.out.println(Arrays.copyOf(tempChars, readCount));
57 }
58 }
59
60
61 inputStreamReader.close();
62 }catch(Exception e) {
63 e.printStackTrace();
64 }
65 }
66
67
68
69 一个字符一个字符地读取
70
71
72 public static void readFileByChars(String fileName) {
73 File file = new File(fileName);
74 try{
75 InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file));
76
77 int tempChar;
78
79 while((tempChar=inputStreamReader.read())!=-1) {
80 System.out.println((char)tempChar);
81 }
82
83
84 inputStreamReader.close();
85 }catch(Exception e) {
86 e.printStackTrace();
87 }
88 }
89
90
91
92 java8读取字节 超级简单
93
94
95 public static byte[] java8ReadBytes(String fileName) throws IOException {
96 return Files.readAllBytes(Paths.get(fileName));
97 }
98
99
100
101 一个字节一个字节地读取
102
103
104 public static void readFileByOneByte(String fileName) {
105 File file = new File(fileName);
106 InputStream inputStream = null;
107
108 try{
109 inputStream = new FileInputStream(file);
110 int tempByte;
111 while( (tempByte=inputStream.read())!=-1) {
112 System.out.println(tempByte);
113 }
114
115 inputStream.close();
116 }catch (IOException e) {
117 System.out.println(e);
118 }
119
120 }
121
122
123
124 一个字节一个字节读取到ByteBuffer
125
126
127 public static byte[] readFileByOneByteToBuffer(String fileName) {
128
129
130 ByteBuffer byteBuffer = ByteBuffer.allocate(1024 * 1024);
131
132 File file = new File(fileName);
133 InputStream inputStream = null;
134
135 try{
136 inputStream = new FileInputStream(file);
137 int tempByte;
138 while( (tempByte=inputStream.read())!=-1) {
139 byteBuffer.put((byte)tempByte);
140 }
141
142 inputStream.close();
143 }catch (IOException e) {
144 System.out.println(e);
145 }
146
147 byteBuffer.flip();
148 System.out.println("one limit:" + byteBuffer.limit());
149 byte[] result = new byte[byteBuffer.limit()];
150 byteBuffer.get(result);
151
152 return result;
153
154
155 }
156
157
158
159 多个字节进行读取
160
161
162 public static void readFileByMultiBytes(String fileName) {
163
164 File file = new File(fileName);
165 InputStream inputStream = null;
166
167 try {
168 byte[] bytes = new byte[50];
169 int byteRead = 0;
170 inputStream = new FileInputStream(fileName);
171
172 while( (byteRead = inputStream.read(bytes))!=-1 ) {
173 System.out.println(byteRead);
174 }
175 }catch(IOException e) {
176 System.out.println(e);
177 }finally {
178 if(inputStream!=null) {
179 try{
180 inputStream.close();
181 }catch(IOException e){
182 System.out.println("iput stream close exception" + e);
183 }
184 }
185 }
186 }
187
188
189
190 读取多个字节到ByteBuffer
191
192
193 public static byte[] readFileByMultiBytesToBuffer(String fileName) {
194
195 ByteBuffer byteBuffer = ByteBuffer.allocate(1024*1024);
196 InputStream inputStream = null;
197
198 try {
199 byte[] bytes = new byte[50];
200 int byteRead = 0;
201 inputStream = new FileInputStream(fileName);
202
203 int count = 0;
204 while( (byteRead = inputStream.read(bytes))!=-1 ) {
205 byteBuffer.put(bytes, 0, byteRead);
206 count+=byteRead;
207 }
208
209 System.out.println("readCount:"+count);
210 }catch(IOException e) {
211 System.out.println(e);
212 }finally {
213 if(inputStream!=null) {
214 try{
215 inputStream.close();
216 }catch(IOException e){
217 System.out.println("iput stream close exception" + e);
218 }
219 }
220 }
221
222 byteBuffer.flip();
223 System.out.println("multi limit:" + byteBuffer.limit());
224 byte[] result = new byte[byteBuffer.limit()];
225 byteBuffer.get(result);
226
227 return result;
228 }
229
2
3
4 public static void java8ReadFileLines(String fileName) throws IOException {
5 List lineList = Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8);
6
7 for(String line:lineList){
8 System.out.println(line);
9 }
10 }
11
12
13
14 一行一行地读取文件
15
16
17 public static void readFileByLines(String fileName) {
18 File file = new File(fileName);
19 BufferedReader reader = null;
20 try {
21 reader = new BufferedReader(new FileReader(file));
22 String line = null;
23 while((line=reader.readLine())!=null ) {
24 System.out.println(line);
25 }
26 }catch (IOException e) {
27
28 }finally {
29 if(reader!=null) {
30 try{
31 reader.close();
32 }catch (IOException e) {
33 ;
34 }
35 }
36 }
37 }
38
39
40
41 一次读取多个字符
42
43
44 public static void readFileByMultiChars(String fileName) {
45 File file = new File(fileName);
46 try{
47 InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file));
48
49 char[] tempChars = new char[30];
50
51 int readCount = 0;
52 while((readCount=inputStreamReader.read(tempChars))!=-1) {
53 if(readCount==tempChars.length) {
54 System.out.println(tempChars);
55 }else{
56 System.out.println(Arrays.copyOf(tempChars, readCount));
57 }
58 }
59
60
61 inputStreamReader.close();
62 }catch(Exception e) {
63 e.printStackTrace();
64 }
65 }
66
67
68
69 一个字符一个字符地读取
70
71
72 public static void readFileByChars(String fileName) {
73 File file = new File(fileName);
74 try{
75 InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file));
76
77 int tempChar;
78
79 while((tempChar=inputStreamReader.read())!=-1) {
80 System.out.println((char)tempChar);
81 }
82
83
84 inputStreamReader.close();
85 }catch(Exception e) {
86 e.printStackTrace();
87 }
88 }
89
90
91
92 java8读取字节 超级简单
93
94
95 public static byte[] java8ReadBytes(String fileName) throws IOException {
96 return Files.readAllBytes(Paths.get(fileName));
97 }
98
99
100
101 一个字节一个字节地读取
102
103
104 public static void readFileByOneByte(String fileName) {
105 File file = new File(fileName);
106 InputStream inputStream = null;
107
108 try{
109 inputStream = new FileInputStream(file);
110 int tempByte;
111 while( (tempByte=inputStream.read())!=-1) {
112 System.out.println(tempByte);
113 }
114
115 inputStream.close();
116 }catch (IOException e) {
117 System.out.println(e);
118 }
119
120 }
121
122
123
124 一个字节一个字节读取到ByteBuffer
125
126
127 public static byte[] readFileByOneByteToBuffer(String fileName) {
128
129
130 ByteBuffer byteBuffer = ByteBuffer.allocate(1024 * 1024);
131
132 File file = new File(fileName);
133 InputStream inputStream = null;
134
135 try{
136 inputStream = new FileInputStream(file);
137 int tempByte;
138 while( (tempByte=inputStream.read())!=-1) {
139 byteBuffer.put((byte)tempByte);
140 }
141
142 inputStream.close();
143 }catch (IOException e) {
144 System.out.println(e);
145 }
146
147 byteBuffer.flip();
148 System.out.println("one limit:" + byteBuffer.limit());
149 byte[] result = new byte[byteBuffer.limit()];
150 byteBuffer.get(result);
151
152 return result;
153
154
155 }
156
157
158
159 多个字节进行读取
160
161
162 public static void readFileByMultiBytes(String fileName) {
163
164 File file = new File(fileName);
165 InputStream inputStream = null;
166
167 try {
168 byte[] bytes = new byte[50];
169 int byteRead = 0;
170 inputStream = new FileInputStream(fileName);
171
172 while( (byteRead = inputStream.read(bytes))!=-1 ) {
173 System.out.println(byteRead);
174 }
175 }catch(IOException e) {
176 System.out.println(e);
177 }finally {
178 if(inputStream!=null) {
179 try{
180 inputStream.close();
181 }catch(IOException e){
182 System.out.println("iput stream close exception" + e);
183 }
184 }
185 }
186 }
187
188
189
190 读取多个字节到ByteBuffer
191
192
193 public static byte[] readFileByMultiBytesToBuffer(String fileName) {
194
195 ByteBuffer byteBuffer = ByteBuffer.allocate(1024*1024);
196 InputStream inputStream = null;
197
198 try {
199 byte[] bytes = new byte[50];
200 int byteRead = 0;
201 inputStream = new FileInputStream(fileName);
202
203 int count = 0;
204 while( (byteRead = inputStream.read(bytes))!=-1 ) {
205 byteBuffer.put(bytes, 0, byteRead);
206 count+=byteRead;
207 }
208
209 System.out.println("readCount:"+count);
210 }catch(IOException e) {
211 System.out.println(e);
212 }finally {
213 if(inputStream!=null) {
214 try{
215 inputStream.close();
216 }catch(IOException e){
217 System.out.println("iput stream close exception" + e);
218 }
219 }
220 }
221
222 byteBuffer.flip();
223 System.out.println("multi limit:" + byteBuffer.limit());
224 byte[] result = new byte[byteBuffer.limit()];
225 byteBuffer.get(result);
226
227 return result;
228 }
229
若转载请注明出处!若有疑问,请回复交流!