字节输入流
==Inputstream==
package com.bilibili;
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
/**
* @author WangYH
* @version 2021.1.3
* @date 2023/4/7 15:19
*/
public class Main {
public static void main(String[] args) {
String path = "C:" + File.separator + "Users" + File.separator + "i'li'ke'stay"+File.separator + "Desktop"+File.separator + "java_IO_test" + File.separator + "hello.txt";
try(FileInputStream stream = new FileInputStream(path)) {
//字节流
byte[] b1 = new byte[stream.available()];
//返回可读字节
stream.read(b1,0,3);
//仅限于纯文本文件,其他的可能会乱码
System.out.println(stream.available());
System.out.println(new String(b1));
//跳过指定字节数,这个流是一次性的,不能反向
stream.skip(4);
System.out.println((char) stream.read());
}catch (IOException e) {
e.printStackTrace();
}
}
}
字节输出流
package com.bilibili;
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
/**
* @author WangYH
* @version 2021.1.3
* @date 2023/4/7 15:19
*/
public class Main {
public static void main(String[] args) {
String path = "C:" + File.separator + "Users" + File.separator + "i'li'ke'stay"+File.separator + "Desktop"+File.separator + "java_IO_test" + File.separator + "hello.txt";
try(FileOutputStream stream = new FileOutputStream(path,true)) {
//初始化时,加入true ,表示追加模式
//字节流
stream.write("hello,world!".getBytes(),3,3);
//刷新操作,将缓冲区数据写入指定路径,立即生效
stream.flush();
}catch (IOException e) {
e.printStackTrace();
}
}
}
字节流实现文件拷贝
package com.bilibili;
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
/**
* @author WangYH
* @version 2021.1.3
* @date 2023/4/7 15:19
*/
public class Main {
public static void main(String[] args) {
String path = "D:" + File.separator + "Download" + File.separator + "NeverBeFar.mp4";
try (FileInputStream input = new FileInputStream(path)) {
//创建输入输出流
FileOutputStream output = new FileOutputStream("copy.mp4");
byte[] buffer = new byte[1024];
int len;
while ((len = input.read(buffer)) != -1){
output.write(buffer, 0, len);
}
//大小一样,正常执行,拷贝完成
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
字符输入流
package com.bilibili;
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
/**
* @author WangYH
* @version 2021.1.3
* @date 2023/4/7 15:19
*/
public class Main {
public static void main(String[] args) {
//字符流,只适合读取纯文本文件,因为是以一个具体的字符读取,其他类型不适用
String path = "C:" + File.separator + "Users" + File.separator + "i'li'ke'stay"+File.separator + "Desktop"+File.separator + "java_IO_test" + File.separator + "hello.txt";
try(FileReader reader = new FileReader(path)) {
//读取一个字符
System.out.println((char) reader.read());
System.out.println((char) reader.read());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
字符输出流
package com.bilibili;
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
/**
* @author WangYH
* @version 2021.1.3
* @date 2023/4/7 15:19
*/
public class Main {
public static void main(String[] args) {
//字符流,只适合读取纯文本文件,因为是以一个具体的字符读取,其他类型不适用
String path = "C:" + File.separator + "Users" + File.separator + "i'li'ke'stay"+File.separator + "Desktop"+File.separator + "java_IO_test" + File.separator + "hello.txt";
try(FileWriter writer = new FileWriter(path,true)) {
//以字符作为操作单位
writer.write("Hello World!");
writer.append("hello,").append("world");
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
字符流拷贝
package com.bilibili;
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
/**
* @author WangYH
* @version 2021.1.3
* @date 2023/4/7 15:19
*/
public class Main {
public static void main(String[] args) {
//字符流,只适合读取纯文本文件,因为是以一个具体的字符读取,其他类型不适用
String path = "C:" + File.separator + "Users" + File.separator + "i'li'ke'stay"+File.separator + "Desktop"+File.separator + "java_IO_test" + File.separator + "hello.txt";
try(FileReader reader = new FileReader(path)) {
FileWriter writer = new FileWriter("copy.txt");
char[] ch = new char[3];
int len = reader.read(ch);
while (len != -1) {
//这里输出一定要按实际长度来复制,不要定义一个固定长度
writer.write(ch, 0, len);
len = reader.read(ch);
}
writer.close();
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}