Experiment four java input and output processing

table of Contents

1. The purpose of the experiment

2. Experimental code

1. Use Java's input and output streams to read out the contents of a text file line by line, and add line numbers in sequence every time a line is read, and write it into another file.

2. Use RandomAccessFile stream to read a text file upside down.

3. Please use byte stream without buffer and with buffer to copy picture (or audio or video) files.

4. Please use the character stream without buffer and with buffer to copy text files.

One word per text


1. The purpose of the experiment

1. Master the overall structure of input and output streams;

2. Master the concept of flow;

3. Master the construction methods and common methods of FileInputStream , FileOutputStream , FileReader , and FileWriter ;

4. Understand the use of various streams (including file streams, pipeline streams, connecting files, filtering streams, serialization of objects, and random access).

2. Experimental code

1. Use Java's input and output streams to read out the contents of a text file line by line , and add line numbers in sequence every time a line is read, and write it into another file.

package 作业练习.test4;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.io.File;
public class FileScanner {
    public static void main(String[] args) throws Exception{
        System.out.print("请输入文件名:");
        Scanner reader = new Scanner(System.in);
        String fileName = reader.nextLine();
        File f = new File("E:\\Intellij IDEL\\project\\src\\"+fileName);
        Scanner fi = new Scanner(f);
        //输出:
        String sLine = null;
        int index  = 0;
        while(fi.hasNext()) {
            sLine = fi.nextLine();
            System.out.println(++index + " " + sLine);
            try {
                BufferedWriter out = new BufferedWriter(new FileWriter("test1.txt"));
                out.write(index + " " + sLine);
            } catch (IOException e) {
            }
        }
        System.out.println("文件创建成功!");
    }
}

2. Use RandomAccessFile stream to read a text file upside down.

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

3. Please use byte stream without buffer and with buffer to copy picture (or audio or video) files.

Requirements: (1) Use byte streams FileInputStream and FileOutputStream to achieve replication;

(2) When defining the byte buffer size, you can try to copy buffers of different sizes such as 16 bytes, 256 bytes, and 1024 bytes.

(3) Please count and analyze the time it takes to copy files in different ways.

 

package 作业练习.test4;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class App14_3 {
    public static void main(String[] args) {
        File reader = new File("E:\\Intellij IDEL\\project\\src\\test4\\1.png");
        File writer = new File("\\Intellij IDEL\\project\\src\\test4\\2.png");
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(reader);
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        BufferedInputStream bis = new BufferedInputStream(fis);
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(writer);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        byte[] b = new byte[256];
        int len = -1;
        try {
            while ((len = bis.read(b)) != -1) {
                bos.write(b, 0, len);
                bos.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bos.close();
                fos.close();
                bis.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
package 作业练习.test4;

import java.io.*;
public class test3 {
    public static void main(String []args) throws IOException
    {
// 带缓冲区的字节流拷贝一个文本文件
  FileInputStream fin =new FileInputStream("E:\\Intellij IDEL\\project\\src\\test4\\test.txt");
  FileOutputStream fout =new FileOutputStream("E:\\Intellij IDEL\\project\\src\\test4\\test1.txt");
  byte buf[] =new byte[2014];   //创建字节数组,作为临时缓冲
  if(fin.read(buf)!=-1)
  {
   fout.write(buf);
  }
  System.out.println("文件复制成功");
  fin.close();
  fout.close();



  /*带缓冲区的字符流拷贝一个文本文件
  FileReader fin =new FileReader("E:\Intellij IDEL\project\src\test4\test2.txt");
  BufferedReader din=new BufferedReader(fin) ;
  FileWriter fou =new FileWriter("E:\Intellij IDEL\project\src\test4\test.txt");
  BufferedWriter dou =new BufferedWriter(fou);
  char c[]=new char[1024];  //创建字符数组
  din.read(c);
  fou.write(c);
  System.out.println("文件复制成功");
  din.close();
  fou.close();
  fin.close();
  dou.close();
  */
    }
}

 

4. Please use the character stream without buffer and with buffer to copy text files.

Claim:

(1) Use character stream FileReader and FileWriter to realize copying;

(2) When defining the size of the character buffer, you can try to copy buffers of different sizes such as 16 characters, 256 characters, and 1024 characters.

package 作业练习.test4;
import java.io.*;
public class App14_5 {
    static App14_5 test=new App14_5();
    public static String  openFile(String fileName){      //打开文件
        StringBuffer sb=null;
        FileInputStream fis=null;
        try {
            fis=new FileInputStream(fileName);  ; //实例化输入流对象
            byte b[]=new byte[1024];
            int len;
            sb=new StringBuffer();
            while( (len = fis.read(b))!=-1 ){         //读文件并判断是否到达文件尾
                String str=new String(b,0,len);
                sb.append(str);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
        }
        return sb.toString();
    }
    public static boolean saveFile(String fileName,String content) throws IOException{
        boolean state=false;
        FileOutputStream fos=null;
        try {
            fos=new FileOutputStream(fileName); //实例化输出流对象
            //把content写入到文件中
            state=true;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
        }
        return state;
    }
    public static boolean copyFile(String sourceFileName,String destinationFifleName){
        boolean sate =false;
        InputStream fis=null;
        OutputStream fos=null;
        try {
            fis=new FileInputStream(sourceFileName);
            fos=new FileOutputStream(destinationFifleName);
            int len;
            byte buffer[]=new byte[1024];
            //此处请填写多行
            len=fis.read(buffer);
            String str1=new String(buffer,0,len);
            byte[] b = str1.getBytes();
            fos.write(b);
            sate =true;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try {
                if(fis!=null) fis.close();
                if(fos!=null) fos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return sate;
    }
    public static void main (String args[]) {
        App14_5 test=new App14_5();
        test.copyFile("E:\\Intellij IDEL\\project\\src\\test4\\test.txt",
                "E:\\Intellij IDEL\\project\\src\\test4\\test3.txt");
    }
}
}

One word per text

Only by learning to input rationally and output perceptually will there be a good cycle!

The lucky color in this article is purple! Every thought is a deep purple memory!

Guess you like

Origin blog.csdn.net/weixin_47723732/article/details/112939902