public static void main(String [] agrs){
//创建写入目标文件
File file=new File("E:\\112.txt");
FileInputStream fileInputStream=null;
FileOutputStream fileOutputStream= null;
try {
//获取文件输入流
fileInputStream=new FileInputStream(new File("E:\\11.txt"));
fileOutputStream = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
int count;
//fileInputStream.read() 读取方法,一次读取一个字节
while((count=fileInputStream.read())>-1){
System.out.println((char)count);
//目标文件写入
fileOutputStream.write(count);
}
//强制刷新,输出缓冲区中的内容,否则会导致丢失数据
fileOutputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
fileInputStream.close();;
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
构造方法
通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的 File 对象 file 指定。
通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的路径名 name 指定。
一次读取一个字节,返回下一个数据的字节;如果已发文件末尾,则返回-1
byte[] buff = new byte [1024];
一次读取一个字节数组,指定数据的长度是1024的倍数,使用缓冲数组,可以有效的减少对硬件的io读写次数.
关闭此文件输入流并释放与此流有关的所有系统资源。
构造方法
创建一个向指定 File 对象表示的文件中写入数据的文件输出流。
append==true 从文件末尾写入
一次写一个字节 b- 要写入的字节。
一次写一个字节数组
一次写一部分字节数组
强制刷新缓冲区内容,防止数据丢失
关闭此文件输出流并释放与此流有关的所有系统资源。如果是包含在缓冲流中则不需要关闭,关闭缓冲流就会自动关闭.
public static void main(String [] agrs){
//创建写入目标文件
File file=new File("E:\\360MoveData\\Users\\Administrator\\Desktop\\work\\版本管理\\112.txt");
InputStream inputStream=null;
if(file.exists()){
file.delete();
}
Reader reader=null;
Writer writer= null;
try {
/读入字节流
inputStream=new FileInputStream("E:\\360MoveData\\Users\\Administrator\\Desktop\\work\\版本管理\\11.txt");
//获取文件输入流,使用FieldWriter/FileReader父类. InputStreamReader/OutputStreamWriter 指定编码集,否者读出乱码,
reader=new InputStreamReader(inputStream,"GB2312");
//写入文件的编码格式默认为系统个人,cmd->chcp 可查看系统默认编码格式
writer = new OutputStreamWriter(new FileOutputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
int count;
//可以使用缓冲数组
//char[] buff = new char[1024];
while((count=reader.read())>-1){
System.out.println((char)count);
//目标文件写入
writer.write(count);
}
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
writer.close();;
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
写入一个字符
写入一个字符串
写入一个字符数组
写入一个字符数组的部分
写入一个字符串的部分
强制刷新缓冲区
public static void main(String agrs []) throws IOException {
//获取文件输入流
//读入的缓冲区
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(new FileInputStream("E:\\360MoveData\\Users\\Administrator\\Desktop\\work\\版本管理\\11.txt"),"GBK"));
//写入的缓冲区
BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("E:\\360MoveData\\Users\\Administrator\\Desktop\\work\\版本管理\\112.txt")));
String line=bufferedReader.readLine();
String flag="true";
while(line!=null){
System.out.println(line);
if("true".equals(flag)){
bufferedWriter.write(line);
flag="false";
}else{
//读取整行,不会读取换行符
bufferedWriter.write("\r\n"+line);
}
line=bufferedReader.readLine();
}
bufferedWriter.flush();
bufferedWriter.close();
bufferedReader.close();
}
因篇幅问题不能全部显示,请点此查看更多更全内容