package IO;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
public class ByteStreamDemo {
public static void main(String[] args) {
out();
in();
}
private static void in(){
File file =new File("c:\\test\\2.txt");
try {
InputStream it=new FileInputStream(file);
byte[] bytes=new byte[1024];
StringBuilder sb = new StringBuilder();
int len=-1;
while((len = it.read(bytes))!=-1){
sb.append(new String(bytes));
}
System.out.println(sb);
it.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void out(){
File f=new File("c:\\test\\2.txt");
try {
FileOutputStream fo = new FileOutputStream(f,true);
String content="每一个不曾起舞的日子都是对生命的辜负\r\n";
fo.write(content.getBytes());
fo.close();
System.out.println("成功");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}