一、字节流
父类InputStream和OutputStream, 一次一字节的操作方式,一般用于声音、图像、视频之类的二进制文件
InputStream
- read():int -1
- read(byte[]):int -1
- close():void
- FileInputStream主要用于操作文件
- System.in 主要用于 接收用户输入
OutputStream
- write(int):void
- write(byte[],int,int):void
- close():void
- FileOutputStream主要用于操作文件
- new FileOutputStream(“文件名称”)采用文件覆盖的方式操作
- new FileOutputStream(“文件名称”,boolean是否采用追 加操作)
- System.out和System.err 用于输出到标准输出设备
样例:实现文件夹的拷贝文件夹的深度无法提前预知,所以这里采用递归调用的方式进行操作
private static String str1;
private static String str2;
public static void main(String[] args) throws IOException {
str1 = "E:\\aaa";
str2 = "D:\\aaa";
File ff = new File(str1);
method(ff);
}
public static void method(File ff) throws IOException {
if (ff != null && ff.exists()) {
if (ff.isFile()) {
String st = ff.getAbsolutePath();
String st1 = st.replace(str1, str2);
try (
Reader r =new FileReader(ff);
Writer w =new FileWriter(st1);
){
char[] brr =new char[8192] ;
int a ;
while ((a=r.read(brr))!=-1){
w.write(brr,0,a);
}
}
} else if (ff.isDirectory()) {
String st = ff.getAbsolutePath();
String st1 = st.replace(str1, str2);
File f1 = new File(st1);
if (!f1.exists()) {
ff.mkdirs();
}
File[] arr = ff.listFiles();
if(arr!=null&&arr.length>0) {
for (int a = 0; a < arr.length; a++) {
method(arr[a]);
}
}
}
}
}
二、字符流
一次操作一个字符 一般用于操作文本文件,注意word文档不是字符文件
Reader字符输入流
- read():int 0-65535 -1
- read(char[]):int -1
- close():void
- FileReader用于操作文件,属于节点流 读取指定文件并在控制台上进行显示
Writer字符输出流
- write(int):void
- write(char[],int,int):void
- close()
- FileWriter用于操作文件
- new FileWriter(String leName)
- new FileWriter(String leName, boolean append)默认覆盖,boolean表示是否追加
注意:
- InputStream中的方法 read(byte[]):int; Reader中方法read(char[]):int 如果到达流末尾则-1
- OutputStream中的方法 write(byte[],0,len):void;
- Writer中的方法write(char[],0,len)/write(String)
- 一般在使用中,如果读取数据使用字节流,则写出数据采用的也是字节流;不建议混用,除非引入桥接流
文件流
- FileInputStream(“le-name”) FileInputStream(File)
- FileNotFoundException FileOutputStream(“le-name”)
- FileOutputStream(“le-name”,true) 默认文件覆盖,如果参数true表 示追
- FileWriter(“le-name”) FileWriter(“le-name”,true)
- 一般不使用单字节或者单字符的操作方法,使用数组
- 针对二进制文件不建议使用字符流,建议使用字节流进行操作,否则有可能拷贝文件出现问题:
- 如果针对文本文件则建议使用字符流,因为编码使用比较方便
样例:要求使用模板模式实现对CharNum数组的排序,排序规则为按照出现次数排序,如果出现次数一致,则按照 字母顺序排序
public abstract class Work04 {
public final void sort(Object[] arr) {
for (int a = 1; a < arr.length; a++) {
for (int b = 0; b < arr.length - a; b++) {
if (method(arr[b],arr[b+1])) {
Object aa =arr[b];
arr[b]=arr[b+1];
arr[b+1]=aa;
}
}
}
for(int a=0 ;a<arr.length;a++){
if(arr[a]!=null)
System.out.println(arr[a]);
}
}
public abstract boolean method(Object a ,Object b);
}
public class Work05 extends Work04 {
public boolean method(Object a,Object b){
boolean aaa =false;
if(a!=null&&b!=null){
if(a instanceof CharNum && b instanceof CharNum){
CharNum ch1 =(CharNum)a;
CharNum ch2 =(CharNum)b;
if(ch1.getCount()>ch2.getCount()){
aaa= true;
}
}
}
return aaa;
}
}
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class Work06 {
public CharNum[] method(File ff) throws IOException {
CharNum[] ch = new CharNum[52];
if (ff.exists() && ff.isFile() && ff != null) {
try (
Reader r = new FileReader(ff);
) {
int aa;
while ((aa = r.read()) != -1) {
if (aa >= 'a' && aa <= 'z' || aa >= 'A' && aa <= 'Z') { //找到需要的aa
boolean bb = false;
int a = 0;
for (; a < ch.length; a++) { //遍历这个数组,找到空元素后,追加
if (ch[a] == null) {
bb = true;
break;
} else {
if (ch[a].getNum() == aa) {
ch[a].setCount(ch[a].getCount() + 1);
break;
}
}
}
if (bb) { //bb为真就说明找到了需要添加的元素,下表为a
ch[a] = new CharNum((char) aa);
}
}
}
}
}
return ch;
}
}
public class CharNum {
private char num;
private int count=1;
public CharNum(char num) {
this.num = num;
}
public char getNum() {
return num;
}
public void setNum(char num) {
this.num = num;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
@Override
public String toString() {
return
"字母" + num +
",出现次数为:" + count ;
}
}
import java.io.File;
import java.io.IOException;
public class Work07 {
public static void main(String[] args)throws IOException {
File ff = new File("ff1.txt");
Work06 wo =new Work06();
CharNum[] ch = new CharNum[52];
ch =wo.method(ff);
Work04 wo1 =new Work05();
wo1.sort(ch);
}
}
样例::找到一个大于100k的文件,按照100k为单位,拆分成多个子文件,并且以编号作为文件名结束。
public class Work03 {
private static String str;
public static void main(String[] args) throws IOException {
str = "E:\\aaa\\aaaa.txt";
File ff = new File(str);
InputStream in =new FileInputStream(ff);
String str1 = ff.getAbsolutePath();
int b = 1;
byte[] ch = new byte[100*1024];
int aa;
while ((aa = in.read(ch)) != -1) {
String str2 = str1.substring(0, str1.lastIndexOf("\\")) + "\\" + b + ".txt";
b++;
OutputStream out =new FileOutputStream(str2);
out.write(ch, 0, aa);
}
in.close();
}
}
文件节点流
- FileInputStream和FileOutputStream是文件字节流,是一种节点流 文件字节输入流的构造方法:
- FileInputStream(“文件名称”),如果文件不存在则FileNotFoundException
- FileInputStream(File)
文件字节输出流的构造方法:
- FileOutputStream(“文件名称”) 如果文件不存在则新建文件,如果文件存在则覆盖文件内容
- FileOutputStream(String name文件名称, boolean append是否采用追加方式)
- FileReader和FileWriter类似
内存数组节点
如果文本则使用char[],如果二进制则使用byte[]
构造器方法
- CharArrayReader(char[] buf)其中char[]就是数据的来源,也就是说Reader就是从char[]中读取数据
- CharArrayRead(char[] buf, int oset, int length)
CharArrayWriter用于实现向一个字符数组中写入数据,这个数组可以自动调整大小
ByteArrayInputStream、ByteArrayOutputStream和CharArrayReader以及CharArrayWriter类似,支持操作的 内容不同而已,操作byte[]与char[]
本文详细介绍了Java中的字节流和字符流操作,包括InputStream、OutputStream、Reader和Writer等核心类的功能及使用方法。通过实例展示了文件的复制、排序及拆分等常见任务。
7948

被折叠的 条评论
为什么被折叠?



