Java IO学习笔记+代码(3)

2023-01-31 04:01:09 java 代码 学习笔记
字符流处理

package study.iOStudy;
import java.io.*;
public class ProcesserCharacterStream
{
    public static void main(String[] args)
            throws FileNotFoundException, IOException
    {
        String lineStr;
        FileInputStream fileInStream;
        InputStreamReader inputReader;
        BufferedReader bufReader;
        FileOutputStream fileOutStream;
        OutputStreamWriter outputWriter;
        BufferedWriter bufWriter;
        fileInStream = new FileInputStream("d:\\mydir\\secondFile.txt");
        inputReader = new InputStreamReader(fileInStream);
        bufReader = new BufferedReader(inputReader);
        System.out.println("------------------------------------------------");
        System.out.println("There are file content before modify:");
        while ((lineStr = bufReader.readLine()) != null)
            System.out.println(lineStr);
        bufReader.close();
        inputReader.close();
        fileInStream.close();
        fileOutStream = new FileOutputStream("d:\\mydir\\secondFile.txt");
        outputWriter = new OutputStreamWriter(fileOutStream);
        bufWriter = new BufferedWriter(outputWriter);
        String newStr = new String("Modify the file ! \r\nThis is a nice thing. \r\nWe can write anything.");
        bufWriter.write(newStr, 0, newStr.length());
        System.out.println(newStr);
        bufWriter.close();
        outputWriter.close();
        fileOutStream.close();
        fileInStream = new FileInputStream("d:\\mydir\\secondFile.txt");
        inputReader = new InputStreamReader(fileInStream);
        bufReader = new BufferedReader(inputReader);
        System.out.println("------------------------------------------------");
        System.out.println("There are file content after modify:");
        while ((lineStr = bufReader.readLine()) != null)
            System.out.println(lineStr);
        bufReader.close();
        inputReader.close();
        fileInStream.close();
    }
}
 
接收键盘输入数据

package study.iostudy;
import java.io.*;
public class OutpuTKEyPress
{
    public static void main(String[] args)
    {
        System.out.println("This is a example about acceptance of keyboard.");
        String tempStr = "0";
        try
        {
            InputStreamReader inputReader;
            BufferedReader bufReader;
            inputReader = new InputStreamReader(System.in);
            bufReader = new BufferedReader(inputReader);
            tempStr = bufReader.readLine();
            System.out.println("Input num is: " + tempStr);
        }catch(IOException e)
        {
            e.printStackTrace();
        }
        int n = Integer.parseInt(tempStr);
        int nultiNum = 1;
        for (int i =1; i <= n; i++)
        {
            nultiNum *= i;
        }
        System.out.println("multiply of input number is: " + nultiNum);
    }   
}
 
过滤流

 
package study.iostudy;
import java.io.*;
public class FilterStream
{
    public static void main(String[] args)
    {
        try
        {
            FileInputStream inStream;
            FileOutputStream outStream;
            BufferedInputStream bufInObj;
            BufferedOutputStream bufOutObj;
            DataInputStream dataInObj;
            PushbackInputStream pushObj;
            byte[] tempBuf = new byte[1024];
            int copyLen;
            inStream = new FileInputStream("d:\\mydir\\secondFile.txt");
            outStream = new FileOutputStream("d:\\mydir\\thirdFile.txt");
            bufInObj = new BufferedInputStream(inStream);
            bufOutObj = new BufferedOutputStream(outStream);
            dataInObj = new DataInputStream(inStream);
            System.out.println(dataInObj.readBoolean());
            while ((copyLen = bufInObj.read(tempBuf, 0, 1024)) != -1)
            {
                String copyStr = new String(tempBuf);
                System.out.println(copyStr);
                bufOutObj.write(tempBuf, 0, copyLen);
                bufOutObj.flush();
            }
            int pushData;
            byte[] pushByte = {'o', 'k'};
            pushObj = new PushbackInputStream(
                    new FileInputStream("d:\\mydir\\thirdFile.txt"), 1000);
            while ((pushData = pushObj.read()) != -1)
            {
                if (Character.isLetter((char)pushData))
                {
                    System.out.print((char)pushData);
                }
                else
                {
                    System.out.println();
                    pushObj.unread(pushByte);
                }
            }
        }catch(FileNotFoundException e)
        {
            System.out.println("File not found or persission denied.");
        }catch(IOException e)
        {
            System.out.println("error:" + e);
        }
    }
    
 
}
 
顺序输入流

package study.iostudy;
import java.io.*;
public class SequenceStream
{
    public static void main(String[] args)
    {
        FileInputStream fileStream1, fileStream2;
        try
        {
            String allStr;
            fileStream1 = new FileInputStream("d:\\mydir\\secondFile.txt");
            fileStream2 = new FileInputStream("d:\\mydir\\thirdFile.txt");
            SequenceInputStream seqStream = new SequenceInputStream(
                    fileStream1, fileStream2);
            BufferedInputStream bufObj = new BufferedInputStream(seqStream);
            byte[] bufByte = new byte[1024];
            while (bufObj.read(bufByte, 0, 1024) != -1)
            {
                String tempStr = new String(bufByte);
                System.out.println(tempStr);
            }
        }catch(FileNotFoundException e)
        {
            System.out.println("File not found or no permission.");
        }catch(IOException e)
        {
            System.out.println("error:" + e);
        }
    }
}
 
对象串行化
 

 
package study.iostudy;
 
import java.io.*;
 
class Book implements Serializable
{
    String isbn;
    String name;
    int page;
    String type;
    public Book(String isbn, String name, int page, String type)
    {
        this.isbn = isbn;
        this.name = name;
        this.page = page;
        this.type = type;
    }
}
 
public class SerializableObject implements Serializable
{
    public static void main(String[] args)
            throws IOException, ClassNotFoundException
    {
        Book bookObj = new Book("7-02-016450-1", "Java", 218, "programming");
        FileOutputStream fileOStream = new FileOutputStream("temp.ser");
        ObjectOutputStream objOutStream = new ObjectOutputStream(fileOStream);
        try
        {
            objOutStream.writeObject(bookObj);
            objOutStream.close();
        }catch(IOException e)
        {
            e.printStackTrace();
        }
        bookObj = null;
        FileInputStream fileInStream = new FileInputStream("temp.ser");
        ObjectInputStream objInStream = new ObjectInputStream(fileInStream);
        try
        {
            bookObj = (Book)objInStream.readObject();
            objInStream.close();
        }catch(IOException e)
        {
            e.printStackTrace();
        }
        System.out.println("------------------------------------------------");
        System.out.println("There are infORMation about book:");
        System.out.println("ISBN Number: " + bookObj.isbn);
        System.out.println("Book Name: " + bookObj.name);
        System.out.println("Book Page: " + bookObj.page);
        System.out.println("Book Type: " + bookObj.type);
    }
}

相关文章