创建文件
public class Demo01 {
public static void main(String[] args) throws IOException {
File file1 = new File("d:/1.txt");
// 1 创建文件
file1.createNewFile();
File file2 = new File("d:/demo01");
file2.mkdir();
File file3 = new File("d:/demo01/demo02");
file3.mkdirs();
public class Demo02 {
public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos = null ;
try {
// 1 准备输入输出流
fis = new FileInputStream(new File("d:/jdk-15.0.2_windows-x64_bin.exe"));
fos = new FileOutputStream(new File("C:/jdk15.exe"));
// 2 循环读取操作
int len = 0 ;
byte [] buff = new byte[1024]; // 开辟缓冲区
long start = System.currentTimeMillis();
while ((len = fis.read(buff)) != -1 ) {
fos.write(buff , 0 ,len );
}
long end = System.currentTimeMillis();
System.out.println("时间"+ (end - start) ); // 1265
// 3 关闭输入输出资源
fis.close();
fos.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}
reader和 write
public static void main(String[] args) throws Exception {
// reader和 writer 只能操作文本内容
Reader reader = new FileReader("d:/1.txt");
FileWriter fileWriter = new FileWriter("d:/111.txt");
char [] buff = new char[1024];
int len = 0 ;
while ( (len= reader.read(buff)) != -1 ) {
fileWriter.write( buff , 0 , len ) ;
}
fileWriter.flush();
// 关闭输入资源
reader.close();
fileWriter.close();
}
}
public class Demo03 {
public static void main(String[] args) throws IOException {
method1();
}
public static void method1() throws IOException {
// 创建输入流对象
InputStream is = System.in;
// 转换
InputStreamReader isr = new InputStreamReader(is);
// 创建输出流对象
FileWriter fw = new FileWriter("d:/demo03.txt");
// 读写数据
char[] bys = new char[1024];
int len;
while ((len = isr.read(bys)) != -1) {
fw.write(new String(bys, 0, len));
fw.flush();
}
// 释放资源
is.close();
fw.close();
}
}
输出流装换器
// 输出流装换器
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(""));
OutputStreamWriter osw = new OutputStreamWriter(System.out); // 装换器 stream ->writer
String line = null;
while ((line = (br.readLine())) !=null);{
osw.write(line+"\n");
}
osw.flush();
br.close();
osw.close();
}
}