java第二十七天:杂七杂八的流

阅读: 评论:0

java第二十七天:杂七杂八的流

java第二十七天:杂七杂八的流

java第二十七天:杂七杂八的流

1.__集合与文本文件中数据的相互储存

1.2_把集合中的数据存储到文本文件(掌握)
A:案例演示: 需求:把ArrayList集合中的字符串数据存储到文本文件
/*分析:
- a: 创建一个ArrayList集合
- b: 添加元素
- c: 创建一个高效的字符输出流对象
- d: 遍历集合,获取每一个元素,把这个元素通过高效的输出流写到文本文件中
- e: 释放资源*/public static void main(String[] args) throws IOException {ArrayList<String> list = new ArrayList<>();list.add("张飞");list.add("赵云");list.add("关羽");list.add("马超");list.add("黄忠");list.add("诸葛亮");list.add("刘备");//遍历集合把集合中的数据存储到文本文件中BufferedWriter writer = new BufferedWriter(new FileWriter(&#"));for (String s : list) {writer.write(s);wLine();writer.flush();}writer.close();}
1.2_把文本文件中的数据存储到集合中(掌握)
A:案例演示:	需求:从文本文件中读取数据(每一行为一个字符串数据)到集合中,并遍历集合/*分析:* 		a: 创建高效的字符输入流对象* 		b: 创建一个集合对象* 		c: 读取数据(一次读取一行)* 		d: 把读取到的数据添加到集合中* 		e: 遍历集合* 		f: 释放资源*/public static void main(String[] args) throws IOException {BufferedReader reader = new BufferedReader(new FileReader(&#"));ArrayList<String> list = new ArrayList<>();//读取文本文件,把文本文件中的数据,存到集合中String name=null;while ((name&#adLine())!=null){list.add(name);}reader.close();System.out.println(list);}

2._随机获取文本文件中的姓名(掌握)

A:案例演示:	需求:我有一个文本文件,每一行是一个学生的名字,请写一个程序,每次允许随机获取一个学生名称
/* * 分析:* 		a: 创建一个高效的字符输入流对象* 		b: 创建集合对象* 		c: 读取数据,把数据存储到集合中* 		d: 产生一个随机数,这个随机数的范围是 0 - 集合的长度 . 作为: 集合的随机索引* 		e: 根据索引获取指定的元素* 		f: 输出* 		g: 释放资源*/public static void main(String[] args) throws IOException {BufferedReader reader = new BufferedReader(new FileReader(&#"));ArrayList<String> list = new ArrayList<>();//读取文本文件,把文本文件中的数据,存到集合中String name = null;while ((name = adLine()) != null) {list.add(name);}reader.close();//从集合中随机取一个人Random random = new Random();int index = Int(list.size());String username= (index);System.out.println(username);//解耦:学生的名字数据,你写的随机取人名是程序//我把代码和数据写在一块可以,但是不好,不利于维护。//把程序和数据分离开来,解耦,利用维护}

3.__删除多级文件夹,使用递归

public static void main(String[] args) {//删除多级文件夹,使用递归//封装文件夹File srcFolder = new File("C:\Users\ShenMouMou\Desktop\demo");deleteFolder(srcFolder);}private static void deleteFolder(File srcFolder) {//遍历传过来的这个文件夹,判断他里面是文件还是子文件夹,是文件,就直接删,是文件夹就递归调用File[] files = srcFolder.listFiles();for (File f : files) {if (f.isFile()) {f.delete();}else{//递归deleteFolder(f);}}srcFolder.delete();
}

4.__复制单级文件夹(掌握)

A:案例演示:	需求: 复制D:\course这文件夹到E:\course
- 分析:
- a: 封装D:\course为一个File对象
- b: 封装E:\course为一个File对象,然后判断是否存在,如果不存在就是创建一个目录
- c: 获取a中的File对应的路径下所有的文件对应的File数组
- d: 遍历数组,获取每一个元素,进行复制
- e: 释放资源public static void main(String[] args) throws IOException {//1.封装源文件夹File srcFolder = new File("C:\Users\ShenMouMou\Desktop\demo");System.out.Name());//2.封装目标文件夹File targerFolder = new File("E:\" + Name());//如果目标文件夹不存在,就把他创建出来if (!ists()) {targerFolder.mkdirs();}//进行复制copyFolder(srcFolder,targerFolder);System.out.println("复制完成");}private static void copyFolder(File srcFolder, File targerFolder) throws IOException {//遍历源文件夹,把源文件夹里面的文件,复制到目标文件夹里面去File[] files = srcFolder.listFiles();for (File f : files) {if (f.isFile()) {//进行复制copyFiles(f, targerFolder);}else{//是子文件夹 递归调用//1.封装源子文件夹 f//System.out.println(f);//2.封装目标子文件夹File mbFolder = new File(targerFolder, f.getName());if (!ists()) {mbFolder.mkdirs();}//递归copyFolder(f,mbFolder);}}}//复制文件的方法private static void copyFiles(File f, File targerFolder) throws IOException {FileInputStream in = new FileInputStream(f);//封装目标文件String name = f.getName();boolean b = dsWith(".java");//MyTest.javaif(b){name=name.substring(0,name.lastIndexOf("."))+".txt";//System.out.println(name);}File mbFile = new File(targerFolder,name);FileOutputStream out = new FileOutputStream(mbFile);byte[] bytes = new byte[1024 * 8];int len=0;while ((len&#ad(bytes))!=-1){out.write(bytes,0,len);out.flush();}in.close();out.close();}

5._键盘录入学生信息按照总分排序并写入文本文)(掌握)

A:案例演示:	需求:键盘录入3个学生信息(姓名,语文成绩(chineseScore),数学成绩(mathScore),英语成绩(englishScore)),按照总分从高到低存入文本文件
- 分析: 	
- a: 创建一个学生类: 姓名,语文成绩(chineseScore),数学成绩(mathScore),英语成绩(englishScore)
- b: 因为要排序,所以需要选择TreeSet进行存储学生对象
- c: 键盘录入学生信息,把学生信息封装成一个学生对象,在把学生对象添加到集合中
- d: 创建一个高效的字符输出流对象
- e: 遍历集合,把学生的信息写入到指定的文本文件中
- f: 释放资源
代码:
public static void main(String[] args) throws IOException {/*  A:案例演示:需求:键盘录入3个学生信息(姓名, 语文成绩(chineseScore), 数学成绩(mathScore), 英语成绩(englishScore)),按照总分从高到低存入文本文件*/TreeSet<Student> set= new TreeSet<>(new Comparator<Student>() {@Overridepublic int compare(Student s1, Student s2) {int num = s1.getTotalScore() - s2.getTotalScore();int num2=num==Name()Name()):num;return -num2;}});for (int i = 1; i <= 3; i++) {Scanner sc = new Scanner(System.in);System.out.println("请输入第"+i+"个学生的姓名");String name = sc.nextLine();System.out.println("请输入第" + i + "个学生的语文成绩");int yw= sc.nextInt();System.out.println("请输入第" + i + "个学生的数学成绩");int sx = sc.nextInt();System.out.println("请输入第" + i + "个学生的英语成绩");int yy = sc.nextInt();//创建学生对象,封装每个学生的信息Student student = new Student(name, yw, sx, yy);set.add(student);}//增加一个需求:每一次录入成绩,都保存在一个新的文件中//遍历学生这个集合,把每个学生的信息,存储到文本文件中//写个表头 改成追加写入//用时间来命名这个文件名long ms= System.currentTimeMillis();Date date = new Date(ms);String fileName = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒").format(date);BufferedWriter bfw= new BufferedWriter(new FileWriter(fileName,true));bfw.write("序号t姓名t语文t数学t英语t总分");wLine();bfw.flush();int i=1;for (Student student : set) {bfw.write((i++)+"t"&#Name()+"t"&#ChineseScore()+"t"&#MathScore()+"t"&#EnglishScore()+"t"&#TotalScore());wLine();bfw.flush();}bfw.close();System.out.println("学生成绩保存完成");}
学生类代码:
public class Student{private String name;private int chineseScore;private int mathScore;private int englishScore;public Student() {}public Student(String name, int chineseScore, int mathScore, int englishScore) {this.name = name;this.chineseScore = chineseScore;this.mathScore = lishScore = englishScore;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getChineseScore() {return chineseScore;}public void setChineseScore(int chineseScore) {this.chineseScore = chineseScore;}public int getMathScore() {return mathScore;}public void setMathScore(int mathScore) {this.mathScore = mathScore;}public int getEnglishScore() {return englishScore;}public void setEnglishScore(int englishScore) {lishScore = englishScore;}//获取总分的方法public int getTotalScore(){return chineseScore+mathScore+englishScore;}}

6.__数据输入输出流的概述和使用(了解)

A:数据输入输出流的概述

​ 通过API查看
​ 数据输入和输出流:
数据输入流: DataInputStream
数据输出流: DataOutputStream
特点: 可以写基本数据类型,可以读取基本数据类型

B:案例演示: 数据输入输出流的使用
public static void main(String[] args) throws IOException {/*  数据输入流://以Stream结尾的 一般都是字节流DataInputStream数据输出流:DataOutputStream这对流的特点是能够读写基本数据类型*///writeData();//读取数据:你怎么写的,就怎么读取,顺序不要乱DataInputStream in = new DataInputStream(new FileInputStream(&#"));int i = in.readInt();System.out.println(i);boolean b = in.readBoolean();System.out.println(b);double v = in.readDouble();System.out.println(v);char c = in.readChar();System.out.println(c);String s = in.readUTF();System.out.println(s);in.close();
}private static void writeData() throws IOException {//写入基本数据类型DataOutputStream dos = new DataOutputStream(new FileOutputStream(&#"));dos.writeInt(1000);dos.writeBoolean(true);dos.writeDouble(3.14);dos.writeChar('你');dos.writeUTF("呵呵");dos.close();
}

7.__内存操作流的概述和使用(掌握)

A:内存操作流的概述
a:操作字节数组

​ ByteArrayOutputStream
​ ByteArrayInputStream
​ 此流关闭无效,所以无需关闭

ByteArrayOutputStream
public static void main(String[] args) throws IOException {//内存操作流:不直接去关联文件,他只是在内存中进行数据的读写。//内存操作流有三对:/* 此类实现了一个输出流,其中的数据被写入一个 byte 数组。缓冲区会随着数据的不断写入而自动增长。可使用 toByteArray () 和 toString () 获取数据。关闭 ByteArrayOutputStream 无效。*//* ByteArrayOutputStream()创建一个新的 byte 数组输出流*/ByteArrayOutputStream bos = new ByteArrayOutputStream();String str="好好学习";String str2="天天向上";String str3="爱生活";String str4="爱Java";byte[] bytes = Bytes();//字节数组1byte[] bytes1 = Bytes();//字节数组2byte[] bytes2 = Bytes();//字节数组3byte[] bytes3 = Bytes();//字节数组4//把上面4个字节数组中的数据,统一写入ByteArrayOutputStream 自己所维护的那个字节数组中国bos.write(bytes);bos.write(bytes1);bos.write(bytes2);bos.write(bytes3);//取出ByteArrayOutputStream 他所维护的那个字节数组中的字节数据byte[] allByte = ByteArray();//我把字节数组还原成字符串String s = new String(allByte);System.out.println(s);//你要直接去成字符串String s1 = String();System.out.println(s1);
}
ByteArrayInputStream
public static void main(String[] args) throws IOException {/* ByteArrayInputStream(byte[] buf)创建一个 ByteArrayInputStream,使用 buf 作为其缓冲区数组。*/ByteArrayOutputStream bos = new ByteArrayOutputStream();String str = "好好学习";String str2 = "天天向上";String str3 = "爱生活";String str4 = "爱Java";byte[] bytes = Bytes();//字节数组1byte[] bytes1 = Bytes();//字节数组2byte[] bytes2 = Bytes();//字节数组3byte[] bytes3 = Bytes();//字节数组4//把上面4个字节数组中的数据,统一写入ByteArrayOutputStream 自己所维护的那个字节数组中国bos.write(bytes);bos.write(bytes1);bos.write(bytes2);bos.write(bytes3);//取出ByteArrayOutputStream 他所维护的那个字节数组中的字节数据byte[] allByte = ByteArray();ByteArrayInputStream bis = new ByteArrayInputStream(allByte);/* byte[] epmtyBytes = new byte[1024];int len = ad(epmtyBytes, 0, 3);System.out.println(new String(epmtyBytes,0,len));
*/byte[] cachBytes = new byte[1024*8];int len = ad(cachBytes);System.out.println(new String(cachBytes, 0, len));bos.close();bis.close();}
b:操作字符数组

​ CharArrayWrite
​ CharArrayReader

public static void main(String[] args) throws IOException {/* b:操作字符数组CharArrayWrite  他维护的缓冲区是一个字符数组CharArrayReader*/CharArrayWriter charArrayWriter = new CharArrayWriter();String str = "好好学习";String str2 = "天天向上";String str3 = "爱生活";String str4 = "爱Java";charArrayWriter.write(str);charArrayWriter.write(str2);charArrayWriter.write(str3);charArrayWriter.write(str4);//取出CharArrayWrite所维护的字符数组char[] allArray = CharArray();String s = new String(allArray);System.out.println(s);String s1 = String();System.out.println(s1);
}
c:操作字符串

​ StringWriter
​ StringReader

public static void main(String[] args) {/* c:操作字符串StringWriter  他底层用的是SStringBuffer 来充当缓冲区StringReader*/StringWriter writer = new StringWriter();writer.write("abc");writer.write("abc");writer.write("abc");writer.write("abc");writer.write("abc");String s = String();System.out.println(s);
}
B:案例演示: 内存操作流的使用

构造方法: public ByteArrayOutputStream()

把多首歌合并成一首歌
public static void main(String[] args) throws IOException {//把多首歌合并成一首歌FileInputStream in2 = new FileInputStream("许巍 - 蓝莲花.mp3");FileInputStream in1 = new FileInputStream("许巍 - 曾经的你.mp3");ArrayList<FileInputStream> list = new ArrayList<>();list.add(in1);list.add(in2);//遍历集合,读取两首歌的字节数据,并把两首歌的字节数据,放到内存操作流所维护的字节数组中ByteArrayOutputStream bos = new ByteArrayOutputStream();byte[] bytes=new byte[1024*8];int len=0;for (FileInputStream fin : list) {while ((len&#ad(bytes))!=-1){bos.write(bytes,0,len);}fin.close();}//取出两首歌的字节数据byte[] allBytes = ByteArray();//把两首歌的字节数据写到一个文件中ByteArrayInputStream bis = new ByteArrayInputStream(allBytes);FileOutputStream out = new FileOutputStream("C:\Users\ShenMouMou\Desktop\许巍歌曲大连唱.mp3");byte[] bytes2 = new byte[1024 * 8];int len2 = 0;while ((len2 = ad(bytes2)) != -1) {out.write(bytes2, 0, len2);}out.close();System.out.println("合并完成");
}
把多个文件合并成一个文件
public static void main(String[] args) throws IOException {//把多个文件合并成一个文件,没有必要使用内存操作流,来中转字节数据FileInputStream in2 = new FileInputStream("许巍 - 蓝莲花.mp3");FileInputStream in1 = new FileInputStream("许巍 - 曾经的你.mp3");FileOutputStream out = new FileOutputStream("C:\Users\ShenMouMou\Desktop\许巍歌曲大连唱222.mp3");ArrayList<FileInputStream> list = new ArrayList<>();list.add(in1);list.add(in2);byte[] bytes = new byte[1024 * 8];int len = 0;for (FileInputStream fin : list) {while ((len = ad(bytes)) != -1) {out.write(bytes, 0, len);}fin.close();}out.close();System.out.println("合并完成");
}

8.__打印流

8.1_打印流的概述和特点以及作为Writer的子类使用(理解)
A:打印流的概述通过API查看字节流打印流字符打印流
B:打印流的特点a: 打印流只能操作目的地,不能操作数据源(不能进行读取数据)- b: 可以操作任意数据类型的数据 调用print() 方法可以写任意数据类型- c: 如果我们启用自动刷新,那么在调用println、printf 或 format 方法中的一个方法的时候,会完成自动刷新/**通过以下构造创建对象 能够启动自动刷新  然后调用println、printf 或 format 方法中的一个方法的时候,会完成自动刷新* public PrintWriter(OutputStream out,  boolean autoFlush)	 启动 自动刷新* public PrintWriter(Writer out,  boolean autoFlush)		启动自动刷新*/- d: 这个流可以直接对文件进行操作(可以直接操作文件的流: 就是构造方法的参数可以传递文件或者文件路径)C:案例演示:	PrintWriter作为Writer的子类使用
8.2打印流代码练习——字符打印流
public static void main(String[] args) throws FileNotFoundException {//打印流:就是单个的,只能往出写数据,//打印流:字符打印流。字节打印流//字符打印流//PrintWriter/* PrintWriter(File file)使用指定文件创建不具有自动行刷新的新 PrintWriter。PrintWriter(String fileName)创建具有指定文件名称且不带自动行刷新的新 PrintWriter。*/PrintWriter writer = new PrintWriter(new File(&#"));writer.write("abc");writer.write("rn");//写成并换行writer.println("aaaaaaaaaaaa");writer.println(200);writer.println(true);writer.print("aaaa2222");writer.write("rn");writer.print("aaaa2222");writer.flush();writer.close();}
8.3_PrintWriter实现自动刷新和换行(理解)
A:案例演示:PrintWriter实现自动刷新和换行PrintWriter pw = new PrintWriter(new FileWriter(&#") , true) ;pw.println(true) ;pw.println(100) ;pw.println("中国") ;public static void main(String[] args) throws IOException {/*  PrintWriter(OutputStream out, boolean autoFlush)通过现有的 OutputStream 创建新的 PrintWriter。PrintWriter(Writer out, boolean autoFlush)创建新 PrintWriter。*///参数2:true开启自动刷新/*   如果启用了自动刷新,则只有在调用 println、printf 或 format的其中一个方法时才可能完成此操作,而不是每当正好输出换行符时才完成。*/PrintWriter writer = new PrintWriter(new FileWriter(&#"), true);// writer.write("abc");//手动刷新// writer.flush();writer.print("abc555555");writer.println("cccc");writer.println("cccc");writer.println("cccc");writer.println("cccc");writer.println("cccc");writer.close();}
8.3打印流代码练习——字节打印流
public static void main(String[] args) throws IOException {//PrintStream 字节打印流 单个的,只能输出数据。/*PrintStream(File file)创建具有指定文件且不带自动行刷新的新打印流。
PrintStream(File file, String csn)创建具有指定文件名称和字符集且不带自动行刷新的新打印流。
PrintStream(OutputStream out)创建新的打印流。PrintStream(String fileName)创建具有指定文件名称且不带自动行刷新的新打印流。
PrintStream(String fileName, String csn)创建具有指定文件名称和字符集且不带自动行刷新的新打印流。*///手动new 一个字节打印流,他就是关联一个文件,当你调用println() 方法时,他是把数据写入到所关联的文件中PrintStream stream = new PrintStream(&#");stream.write("nihao".getBytes());stream.println("sbc");stream.println(2000);stream.println(true);stream.flush();stream.close();System.out.println("==================================");//我们通过System类中的静态变量in 可以获取出一个PrintStream字节打印流 调用输出的方法时输出的目的地是屏幕// System类的属性 out标准”输出流。此流已打开并准备接受输出数据。通常,此流对应于显示器输出PrintStream out = System.out;out.write("asdfasdfasdfasdf".getBytes());out.println("asdfasdf");out.println("asdfasdf");out.println("asdfasdf");out.println("asdfasdf");out.println("asdfasdf");}
8.4_打印流复制文本文件(理解)
A:案例演示:	打印流复制文本文件
分析:
- 这个打印流只能进行写数据,不能进行读取数据,那么我们应该找一个可以读取文本文件中的的数据的流对象进行读取操作.
- 而我们非常喜欢高效的流对象,于是我们可以使用BufferedReader进行读取数据.*public static void main(String[] args) throws IOException {/* A:案例演示:打印流复制文本文件*///我们使用  BufferedReader和 PrintWriter配合来复制文本文件BufferedReader reader = new BufferedReader(new FileReader("Mytest.java"));//开启自动刷新PrintWriter writer = new PrintWriter(new FileWriter(&#"),true);String line=null;while ((line&#adLine())!=null){writer.println(line);}reader.close();writer.close();}

9._标准输入输出流概述和输出语句的本质(理解)

A:标准输入输出流概述

​ 在System这个类中存在两个静态的成员变量:

  • public static final InputStream in: 标准输入流, 对应的设备是键盘
public static void main(String[] args) throws FileNotFoundException {//in//public static final InputStream in“标准”输入流。// 此流已打开并准备提供输入数据。通常,此流对应于键盘输入或者由主机环境或用户指定的另一个输入源。//Scanner scanner = new Scanner(System.in);/*Scanner(File source)构造一个新的 Scanner,它生成的值是从指定文件扫描的。Scanner(InputStream source)构造一个新的 Scanner,它生成的值是从指定的输入流扫描的。*///Scanner 关联文件,从文件中一行一行扫描数据Scanner scanner = new Scanner(new File("Mytest.java"));while (scanner.hasNextLine()){String s = Line();System.out.println(s);}}
  • public static final PrintStream out: 标准输出流 , 对应的设备就是显示器

  • System.in的类型是InputStream.
    System.out的类型是PrintStream是OutputStream的孙子类FilterOutputStream 的子类.

    B:案例演示: 输出语句的本质

10._二种方式实现键盘录入(理解)

A:Scanner
public static void main(String[] args) throws IOException {//我们用Scanner和PrintWriter 配合来复制文件Scanner scanner = new Scanner(new File("Mytest.java"));//开启自动刷新PrintWriter writer = new PrintWriter(new FileWriter("hehe.java"), true);while (scanner.hasNextLine()) {String s = Line();//System.out.println(s);writer.println(s);}scanner.close();writer.close();
}
B:BufferedReader的readLine方法。

​ BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

public static void main(String[] args) throws IOException {//键盘录入的第二种方式//in//public static final InputStream in“标准”输入流。// 此流已打开并准备提供输入数据。通常,此流对应于键盘输入或者由主机环境或用户指定的另一个输入源。// Scanner scanner = new Scanner(System.in);BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));while (true) {System.out.println("请输入数据");String s = adLine();//自己定义一个结束标记if ("886".equals(s)) {break;}System.out.println(s);}
}

11._输出语句用字符缓冲流改进(理解)

A:案例演示:	输出语句用字符缓冲流改进
/*** 获取System下的in成员变量*/InputStream in = System.in ;/*** in是一个字节输入流对象,那么我们就可以通过这个字节输入流对象进行读取键盘录入的数据.* 那么我们既然要读取数据,之前我们讲解了两种读取数据的方式:* 	 1. 一次读取一个字节* 	 2. 一次读取一个字节数组* 那么我们在这个地方使用那种读取方式. 经过分析,这两种读取方式都不太合适.因为数据是客户通过键盘录入* 进来的,而我们希望直接读取一行数据. 而既然要读取一行数据,那么我们就需要使用readLine方法,而这个方法* 是属于BufferedReader的方法,而我们就需要创建一个BufferedReader对象进行读取数据.而我们这in有属于* 字节流,而创建BufferedReader对象的时候需要一个字符流,而我们就需要将这个字节流转换成字符流,那么既然* 要对其进行转换,那么就需要使用转换流. 需要使用InputStreamReader*/

12._随机访问流

12.1_随机访问流概述和写出数据(掌握)
A:随机访问流概述

​ RandomAccessFile概述 最大特点 能读能写
​ RandomAccessFile类不属于流,是Object类的子类。但它融合了InputStream和OutputStream的功能。
​ 支持对随机访问文件的读取和写入。

​ RandomAccessFile的父类是Object , 这个流对象可以用来读取数据也可以用来写数据.可以操作任意数据类型的数据.
我们可以通过getFilePointer方法获取文件指针,并且可以通过seek方法设置文件指针
B:案例演示: 随机访问流写出数据

12.2_随机访问流读取数据和操作文件指针(了解)
A:案例演示:	随机访问流读取数据和操作文件指针
public static void main(String[] args) throws IOException {// RandomAccessFile概述 最大特点 能读能写而且有一个文件指针,可以控制指针的位置。/*  此类的实例支持对随机访问文件的读取和写入。随机访问文件的行为类似存储在文件系统中的一个大型 byte 数组。存在指向该隐含数组的光标或索引,称为文件指针;输入操作从文件指针开始读取字节,并随着对字节的读取而前移此文件指针。*//* RandomAccessFile(String name, String mode)创建从中读取和向其中写入(可选)的随机访问文件流,该文件具有指定名称。
*/// writeData();RandomAccessFile read = new RandomAccessFile(&#", "rw");//你怎么写的就怎么读取,顺序不要乱int i = adInt();System.out.println(i);long filePointer = FilePointer();System.out.println("文件指针的位置"+filePointer);boolean b = adBoolean();System.out.println(b);filePointer = FilePointer();System.out.println("文件指针的位置" + filePointer);double v = adDouble();System.out.println(v);filePointer = FilePointer();System.out.println("文件指针的位置" + filePointer);String s = adUTF();System.out.println(s);filePointer = FilePointer();System.out.println("文件指针的位置" + filePointer);//我们可以移动文件指针read.seek(0);int i1 = adInt();System.out.println(i1);read.seek(13); //设置文件指针的位置String s1 = adUTF();System.out.println(s1);read.close();}private static void writeData() throws IOException {//rw 可读可写RandomAccessFile write = new RandomAccessFile(&#", "rw");write.writeInt(100);write.writeBoolean(true);write.writeDouble(3.14);//会多写两个字节write.writeUTF("你好");write.close();}
复制文件,可以暂停继续
public static void main(String[] args) throws IOException {//复制文件,可以暂停继续//下载:断点下载RandomAccessFile read = new RandomAccessFile("许巍 - 蓝莲花.mp3","rw");File file = new File("C:\Users\ShenMouMou\Desktop\许巍 - 蓝莲花.mp3");RandomAccessFile write = new RandomAccessFile(file, "rw");//严谨性判断,如果上次没复制完的文件,不存在了,就从头开始复制/*  if(!ists()||file.length()<上次记录的字节数){//把文件指针设置为0 从头开始读read.seek(0);//把文件指针设置为0 从头开始写write.seek(0);}else{//从配置文件中读取上次断开的位置read.seek(3001);//从配置文件中读取上次断开的位置write.seek(3301);}*/int by=0;int i=1;try {while ((by&#ad())!=-1){/*模拟一个异常,或模拟用户暂停if(i++>3000){System.out.println(1/0);}*/write.write(by);}} catch (Exception e) {//当复制或下载文件过程当中遇到了异常或用户手动暂停,我们就把文件当前的指针位置,存到一个配置文件中,//为了用户下次继续复制或下载时,把这个位置读取回去,从这个位置开始继续读写。long filePointer = FilePointer();System.out.println(filePointer);PrintWriter printWriter = new PrintWriter(&#");printWriter.println(filePointer);printWriter.flush();printWriter.close();e.printStackTrace();}
}

本文发布于:2024-01-30 13:54:43,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170659408720468.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:十七天   java   杂七杂八
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23