目录
Java高级特性 - IO流
什么是I0流 BC C
字节流-输入输出
字符流-输入输出
复制文件
如果此文章对你有帮助,麻烦点个赞哦~~~
点个关注,追新不迷路~,下一篇——Java集合框架
Java高级特性 - IO流
什么是I0流 BC C
字节流-输入输出
package step2;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Task {
public void task() throws IOException{
/********* Begin *********/
FileInputStream fs = new FileInputStream("src/step2/input/task.txt");
byte[] b = new byte[8];
fs.read(b);
String str = new String(b);
System.out.println(str);
File dir = new File("src/step2/output/");
if(!dir.exists()){
dir.mkdir();
}
FileOutputStream fos = new FileOutputStream("src/step2/output/output.txt");
String out = "learning practice";
byte[] outByte = out.getBytes(); //将字符串转换成字节
fos.write(outByte); //写数据
//释放资源
fs.close();
fos.close();
/********* End *********/
}
}
字符流-输入输出
package step3;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Task {
public void task() throws IOException{
/********* Begin *********/
String file1 = "src/step3/input/input.txt"; //创建文件
FileReader fr = new FileReader(file1); //实例化
char[] ch = new char[8]; //创建数组
fr.read(ch); //将文件的数据读入到数组中(从前到后)
String file2="src/step3/output/output.txt";//创建文件
FileWriter fw = new FileWriter(file2); // 实例化
fw.write(ch); // 读入数组中的数据到文件中(从后到前)
fr.close(); //关闭流
fw.flush(); //刷新流
fw.close(); //关闭流
/********* End *********/
}
}
复制文件
package step4;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Task {
public void task() throws IOException{
/********* Begin *********/
FileReader fr = new FileReader("src/step4/input/input.txt");
FileWriter fw = new FileWriter("src/step4/output/output.txt");
int len = 0;
char[] cha = new char[1024];
while ( (len = fr.read(cha) ) != -1)
{
fw.write(cha , 0 , len);
}
fr.close();
fw.flush();
fw.close();
FileInputStream fs = new FileInputStream("src/step4/input/input.jpg");
FileOutputStream fo = new FileOutputStream("src/step4/output/output.jpg");
int le = 0;
byte[] bt = new byte[1024];
while ( (le = fs.read(bt) ) != -1)
{
fo.write (bt , 0 , le);
}
fs.close();
fo.flush();
fo.close();
/********* End *********/
}
}
如果此文章对你有帮助,麻烦点个赞哦~~~
点个关注,追新不迷路~,下一篇——Java集合框架