您现在的位置是:首页 > 编程语言学习 > 后端编程语言 > 文章正文 后端编程语言

java文件操作输入输出结构详解

2022-07-08 11:54:31 后端编程语言

简介一、实验目的1. 掌握输入输出流的总体结构;2. 掌握流的概念;3. 掌握FileInputStream类、FileOutputStream类、FileReader类、FileWrite...

一、实验目的

  • 1. 掌握输入输出流的总体结构;
  • 2. 掌握流的概念;
  • 3. 掌握FileInputStream类、FileOutputStream类、FileReader类、FileWriter类的构造方法、常用方法的使用;
  • 4. 了解各种流(包括文件流、管道流、连接文件、过滤流、对象的序列化、随机访问)的使用。

二、实验代码

1.使用Java的输入输出

使用Java的输入、输出流将一个文本文件的内容按行读出,每读出一行就顺序添加行号,并写入到另一个文件中。

  1. package 作业练习.test4; 
  2. import java.io.BufferedWriter; 
  3. import java.io.FileWriter; 
  4. import java.io.IOException; 
  5. import java.util.Scanner; 
  6. import java.io.File; 
  7. public class FileScanner { 
  8. public static void main(String[] args) throws Exception{ 
  9. System.out.print("请输入文件名:"); 
  10. Scanner reader = new Scanner(System.in); 
  11. String fileName = reader.nextLine(); 
  12. File f = new File("E:\\Intellij IDEL\\project\\src\\"+fileName); 
  13. Scanner fi = new Scanner(f); 
  14. //输出: 
  15. String sLine = null
  16. int index = 0
  17. while(fi.hasNext()) { 
  18. sLine = fi.nextLine(); 
  19. System.out.println(++index + " " + sLine); 
  20. try { 
  21. BufferedWriter out = new BufferedWriter(new FileWriter("test1.txt")); 
  22. out.write(index + " " + sLine); 
  23. catch (IOException e) { 
  24. System.out.println("文件创建成功!"); 

2.使用RandomAccessFile流将一个文本文件倒置读出

  1. package 作业练习.test4; 
  2. import java.io.*; 
  3. public class test2 { 
  4. public static void main(String []args) throws IOException 
  5. RandomAccessFile file =new RandomAccessFile("E:\\Intellij IDEL\\project\\src\\test4\\test.txt","r"); 
  6. long len =file.length(); 
  7. while(0!=len--) 
  8. file.seek(len); 
  9. char ch =(char)file.read(); 
  10. System.out.print(ch); 
  11. file.close(); 

3.请分别使用不带缓冲区和带缓冲区的字节流复制图片(或者音频或者视频)文件

要求:

  • (1) 使用字节流FileInputStream、FileOutputStream实现复制;
  • (2) 在定义字节缓冲区大小时,可以尝试16字节、256字节、1024字节等不同大小的缓冲区进行复制。
  • (3) 请统计采用不同方式进行文件复制所花的时间,并进行分析。
  1. package 作业练习.test4; 
  2. import java.io.BufferedInputStream; 
  3. import java.io.BufferedOutputStream; 
  4. import java.io.File; 
  5. import java.io.FileInputStream; 
  6. import java.io.FileNotFoundException; 
  7. import java.io.FileOutputStream; 
  8. import java.io.IOException; 
  9. public class App14_3 { 
  10. public static void main(String[] args) { 
  11. File reader = new File("E:\\Intellij IDEL\\project\\src\\test4\\1.png"); 
  12. File writer = new File("\\Intellij IDEL\\project\\src\\test4\\2.png"); 
  13. FileInputStream fis = null
  14. try { 
  15. fis = new FileInputStream(reader); 
  16. catch (FileNotFoundException e1) { 
  17. e1.printStackTrace(); 
  18. BufferedInputStream bis = new BufferedInputStream(fis); 
  19. FileOutputStream fos = null
  20. try { 
  21. fos = new FileOutputStream(writer); 
  22. catch (FileNotFoundException e) { 
  23. e.printStackTrace(); 
  24. BufferedOutputStream bos = new BufferedOutputStream(fos); 
  25. byte[] b = new byte[256]; 
  26. int len = -1
  27. try { 
  28. while ((len = bis.read(b)) != -1) { 
  29. bos.write(b, 0, len); 
  30. bos.flush(); 
  31. catch (IOException e) { 
  32. e.printStackTrace(); 
  33. finally { 
  34. try { 
  35. bos.close(); 
  36. fos.close(); 
  37. bis.close(); 
  38. fis.close(); 
  39. catch (IOException e) { 
  40. e.printStackTrace(); 
  41. package 作业练习.test4; 
  42. import java.io.*; 
  43. public class test3 { 
  44. public static void main(String []args) throws IOException 
  45. // 带缓冲区的字节流拷贝一个文本文件 
  46. FileInputStream fin =new FileInputStream("E:\\Intellij IDEL\\project\\src\\test4\\test.txt"); 
  47. FileOutputStream fout =new FileOutputStream("E:\\Intellij IDEL\\project\\src\\test4\\test1.txt"); 
  48. byte buf[] =new byte[2014]; //创建字节数组,作为临时缓冲 
  49. if(fin.read(buf)!=-1
  50. fout.write(buf); 
  51. System.out.println("文件复制成功"); 
  52. fin.close(); 
  53. fout.close(); 
  54. /*带缓冲区的字符流拷贝一个文本文件 
  55. FileReader fin =new FileReader("E:\Intellij IDEL\project\src\test4\test2.txt"); 
  56. BufferedReader din=new BufferedReader(fin) ; 
  57. FileWriter fou =new FileWriter("E:\Intellij IDEL\project\src\test4\test.txt"); 
  58. BufferedWriter dou =new BufferedWriter(fou); 
  59. char c[]=new char[1024]; //创建字符数组 
  60. din.read(c); 
  61. fou.write(c); 
  62. System.out.println("文件复制成功"); 
  63. din.close(); 
  64. fou.close(); 
  65. fin.close(); 
  66. dou.close(); 
  67. */ 

4.请分别使用不带缓冲区和带缓冲区的字符流复制文本文件

要求:

  • (1) 使用字符流FileReader、FileWriter实现复制;
  • (2) 在定义字符缓冲区大小时,可以尝试16字符、256字符、1024字符等不同大小的缓冲区进行复制。
  1. package 作业练习.test4; 
  2. import java.io.*; 
  3. public class App14_5 { 
  4. static App14_5 test=new App14_5(); 
  5. public static String openFile(String fileName){ //打开文件 
  6. StringBuffer sb=null
  7. FileInputStream fis=null
  8. try { 
  9. fis=new FileInputStream(fileName); ; //实例化输入流对象 
  10. byte b[]=new byte[1024]; 
  11. int len; 
  12. sb=new StringBuffer(); 
  13. while( (len = fis.read(b))!=-1 ){ //读文件并判断是否到达文件尾 
  14. String str=new String(b,0,len); 
  15. sb.append(str); 
  16. catch (Exception e) { 
  17. // TODO Auto-generated catch block 
  18. e.printStackTrace(); 
  19. }finally
  20. return sb.toString(); 
  21. public static boolean saveFile(String fileName,String content) throws IOException{ 
  22. boolean state=false
  23. FileOutputStream fos=null
  24. try { 
  25. fos=new FileOutputStream(fileName); //实例化输出流对象 
  26. //把content写入到文件中 
  27. state=true
  28. catch (Exception e) { 
  29. e.printStackTrace(); 
  30. }finally { 
  31. return state; 
  32. public static boolean copyFile(String sourceFileName,String destinationFifleName){ 
  33. boolean sate =false
  34. InputStream fis=null
  35. OutputStream fos=null
  36. try { 
  37. fis=new FileInputStream(sourceFileName); 
  38. fos=new FileOutputStream(destinationFifleName); 
  39. int len; 
  40. byte buffer[]=new byte[1024]; 
  41. //此处请填写多行 
  42. len=fis.read(buffer); 
  43. String str1=new String(buffer,0,len); 
  44. byte[] b = str1.getBytes(); 
  45. fos.write(b); 
  46. sate =true
  47. catch (Exception e) { 
  48. // TODO Auto-generated catch block 
  49. e.printStackTrace(); 
  50. }finally
  51. try { 
  52. if(fis!=null) fis.close(); 
  53. if(fos!=null) fos.close(); 
  54. catch (IOException e) { 
  55. // TODO Auto-generated catch block 
  56. e.printStackTrace(); 
  57. return sate; 
  58. public static void main (String args[]) { 
  59. App14_5 test=new App14_5(); 
  60. test.copyFile("E:\\Intellij IDEL\\project\\src\\test4\\test.txt"
  61. "E:\\Intellij IDEL\\project\\src\\test4\\test3.txt"); 

 

站点信息