使用FileInputStream和FileReader进行文件的读写,方式都是使用read(buf[])的方式,然后通过while循环进行输出。需要注意的是,FileInputStream读的是字节数组byte[],而FileReader读的是字符数组char[]。这也就是在构造String对象式出现不同的方式的原因。对于byte[]数组,如果不存在中文,那么直接使用
public String(byte[] bytes, int offset, int length)这个构造函数就可以,但是如果有中文,需要给出以何种方式进行构造–>
public String(byte[] bytes, int offset, int length, Charset charset)而相对于字符数组来说,就不要进行这样的转换,因为读取的时候,直接按照字符进行读取。这也就是代码中把从流中读到的数组转换成String对象时使用的方法不同的原因。
下面是代码:
- package com.xueyoucto.xueyou;
- import java.io.*;
- /**
- * Hello world!
- */
- public class App {
- public static void main(String[] args) throws IOException{
- System.out.println(”Hello World!”);
- FileInputStream fileInputStream = new FileInputStream(“d:/123.txt”);
- byte[] bbuf = new byte[1024];
- int hasRead = 0;
- StringBuffer sb = new StringBuffer(“”);
- while((hasRead = fileInputStream.read(bbuf)) != -1){
- sb.append(new String(bbuf, 0, hasRead, “utf-8”));
- }
- fileInputStream.close();
- System.out.println(sb);
- System.out.println(”“);
- System.out.println(”============================================================”);
- FileReader fileReader = new FileReader(“d:/123.txt”);
- char cbuff[] = new char[1024];
- int cHasRead = 0;
- sb.setLength(0);
- while((cHasRead = fileReader.read(cbuff)) != -1){
- sb.append(cbuff,0,cHasRead);
- }
- fileReader.close();
- System.out.println(sb);
- }
- }
package com.xueyoucto.xueyou;
import java.io.*;
/**
* Hello world!
*/
public class App {
public static void main(String[] args) throws IOException{
System.out.println("Hello World!");
FileInputStream fileInputStream = new FileInputStream("d:/123.txt");
byte[] bbuf = new byte[1024];
int hasRead = 0;
StringBuffer sb = new StringBuffer("");
while((hasRead = fileInputStream.read(bbuf)) != -1){
sb.append(new String(bbuf, 0, hasRead, "utf-8"));
}
fileInputStream.close();
System.out.println(sb);
System.out.println("");
System.out.println("============================================================");
FileReader fileReader = new FileReader("d:/123.txt");
char cbuff[] = new char[1024];
int cHasRead = 0;
sb.setLength(0);
while((cHasRead = fileReader.read(cbuff)) != -1){
sb.append(cbuff,0,cHasRead);
}
fileReader.close();
System.out.println(sb);
}
}
/* 使用FileInputStream和FileReader */
public class TestFileInputStream {
public static void main(String[] args) throws IOException {
File file = new File("D://FFF//1234.text");
FileInputStream in = null;
in = new FileInputStream(file);
byte[] bytes = new byte[1024];
int len = 0;
StringBuilder sb = new StringBuilder();
// 使用FileInputStream读取
while((len=in.read(bytes)) != -1){
sb.append(new String(bytes, 0, len, "GBK"));
System.out.println(sb);
}
in.close();
System.out.println("================================");
// 使用FileReader读取
/*FileReader fileReader = new FileReader("D://FFF//1234.text");
char[] charBuff = new char[1024];
int len1 = 0;
sb.setLength(0);
while((len1 = fileReader.read(charBuff)) != -1){
sb.append(charBuff, 0, len1);
System.out.println(sb);
}
fileReader.close();*/ /* 这里使用FileReader读取出现乱码 */
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"GBK"));
StringBuilder sb1 = new StringBuilder();
/*char [] charbuff = new char[1024];
int len1 = 0;
while((len1 = br.read(charbuff)) != -1){
sb1.append(charbuff, 0, len1);
System.out.println(sb1);
}*/ /* 使用InputStreamReader来设置编码 */
StringBuilder stringBuilder = new StringBuilder();
String line = null;
while((line = br.readLine()) != null){
stringBuilder.append(line);
}
System.out.println(stringBuilder);
}
}
运行效果: