IO
IO概述(概念&分类)
字节输出流
OutputStream&FileOutputStream
1.写入数据到文件(FileOutputStream)
public class OutputStreamC {
public static void main(String[] args) throws IOException {
FileOutputStream ops = new FileOutputStream("F:\\test\\aa.txt"); //可以是相对路径(但是我不太敢用)
ops.write(150);
ops.close();
}
}
文件存储的原理和记事本打开原理
2.写多个字节的方法
public class OutputStreamC {
public static void main(String[] args) throws IOException {
FileOutputStream ops = new FileOutputStream("F:\\test\\aa.txt");
byte[] byteArr = {66, 67, 68, 69};
ops.write(byteArr);
ops.close();
}
}public class OutputStreamC {
public static void main(String[] args) throws IOException {
FileOutputStream ops = new FileOutputStream("F:\\test\\aa.txt");
byte[] byteArr = {66, 67, 68, 69};
ops.write(byteArr);
byte[] bytes = "你好".getBytes();
System.out.println(Arrays.toString(bytes));
ops.write(bytes);
ops.close();
ops.close();
}
}
public class OutputStreamC {
public static void main(String[] args) throws IOException {
FileOutputStream ops = new FileOutputStream("F:\\test\\aa.txt");
byte[] byteArr = {66, 67, 68, 69};
ops.write(byteArr,1,2);
ops.close();
}
}
续写和换行
public class OutputStreamC {
public static void main(String[] args) throws IOException {
FileOutputStream ops = new FileOutputStream("F:\\test\\aa.txt",true);
for (int i = 0; i < 10; i++) {
ops.write("你好".getBytes());
ops.write("\r\n".getBytes());
}
}
}
字节输入流
读取字节数据
public class InputStreamClass {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("F:\\test\\aa.txt");
int len = 0; //保存读取的数据,防止多次读取
while((len = fis.read()) != -1) {
System.out.println((char)len);
}
fis.close();
}
}
一次读取一个字节的原理
一次读取多个字节的原理
public class NewDemo {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("F:\\test\\aa.txt");
byte[] bytes = new byte[1024];
int len = 0;
while((len = fis.read(bytes)) != -1) {
System.out.println(new String(bytes,0,len));
}
}
}
练习:文件复制
public class CopyDemo01 {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
FileInputStream fis = new FileInputStream("E:\\Copy1\\aa.txt");
FileOutputStream fos = new FileOutputStream("F:\\Copy2\\cc.txt");
/*int len = 0;
while((len = fis.read()) != -1) {
fos.write(len);
}*/
int len = 0;
byte[] bytes = new byte[1024];
while((len = fis.read(bytes)) != -1) {
fos.write(bytes,0,len);
}
fos.close();
fis.close();
long finish = System.currentTimeMillis();
System.out.println("总共执行了:" + (finish - start) + "毫秒");
}
}
字符输入流
Reader类&FileReader类
public class FileReaderClass {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("F:\\test\\aa.txt");
int len = 0;
/*while ((len = fr.read()) != -1) {
System.out.print((char)len);
}*/
char[] chars = new char[1024];
while ((len = fr.read(chars)) != -1) {
System.out.print(new String(chars,0,len));
}
fr.close();
}
}
字符输出流
Writer类&FileWriter类
public class FileWriterClass {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("F:\\test\\aa.txt");
fw.write(97);
fw.flush();
fw.close(); //close时如果没flush会自动把数据刷到文件中去,用flush跟close对比是,flush后仍然可以write,但是close后流关闭了,不可以再write
//(原来的数据将被覆盖)
}
}
写数据的其他方法
public class FileWriterC {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("H:\\test\\aa.txt");
char[] chars = {'a', 'b', 'c', 'd'};
fw.write(chars);
fw.write(chars,0,2);
fw.write("小新妮妮风间");
fw.write("小新妮妮风间",1,3);
fw.close();
}
}
续写和换行
public class FileWriterC {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("H:\\test\\aa.txt",true);
for (int i = 0; i < 10; i++) {
fw.write("你好" + "\r\n");
}
fw.close();
}
}
try…catch…finally处理流中的异常
public class NewClass {
public static void main(String[] args) {
FileWriter fw = null;
for (int i = 0; i < 10; i++) {
try {
fw = new FileWriter("F:\\test\\aa.txt");
fw.write("小新妮妮风间" + i + "\r\n");
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}