Java中对文件进行读写操作的基本类是什么?

Python08

Java中对文件进行读写操作的基本类是什么?,第1张

Java.io包中包括许多类提供许多有关文件的各个方面操作。\x0d\x0a1 输入输出抽象基类InputStream/OutputStream ,实现文件内容操作的基本功能函数read()、 write()、close()、skip()等;一般都是创建出其派生类对象(完成指定的特殊功能)来实现文件读写。在文件读写的编程过程中主要应该注意异常处理的技术。 \x0d\x0a2 FileInputStream/FileOutputStream: \x0d\x0a用于本地文件读写(二进制格式读写并且是顺序读写,读和写要分别创建出不同的文件流对象); \x0d\x0a本地文件读写编程的基本过程为: \x0d\x0a① 生成文件流对象(对文件读操作时应该为FileInputStream类,而文件写应该为FileOutputStream类); \x0d\x0a② 调用FileInputStream或FileOutputStream类中的功能函数如read()、write(int b)等)读写文件内容; \x0d\x0a③ 关闭文件(close())。 \x0d\x0a3 PipedInputStream/PipedOutputStream: \x0d\x0a用于管道输入输出(将一个程序或一个线程的输出结果直接连接到另一个程序或一个线程的输入端口,实现两者数据直接传送。操作时需要连结); \x0d\x0a4管道的连接: \x0d\x0a方法之一是通过构造函数直接将某一个程序的输出作为另一个程序的输入,在定义对象时指明目标管道对象 \x0d\x0aPipedInputStream pInput=new PipedInputStream() \x0d\x0aPipedOutputStream pOutput= new PipedOutputStream(pInput) \x0d\x0a方法之二是利用双方类中的任一个成员函数 connect()相连接 \x0d\x0aPipedInputStream pInput=new PipedInputStream() \x0d\x0aPipedOutputStream pOutput= new PipedOutputStream() \x0d\x0apinput.connect(pOutput) \x0d\x0a5 管道的输入与输出: \x0d\x0a输出管道对象调用write()成员函数输出数据(即向管道的输入端发送数据);而输入管道对象调用read()成员函数可以读起数据(即从输出管道中获得数据)。这主要是借助系统所提供的缓冲机制来实现的。 \x0d\x0a6随机文件读写: \x0d\x0aRandomAccessFile类(它直接继承于Object类而非InputStream/OutputStream类),从而可以实现读写文件中任何位置中的数据(只需要改变文件的读写位置的指针)。 \x0d\x0a随机文件读写编程的基本过程为: \x0d\x0a① 生成流对象并且指明读写类型; \x0d\x0a② 移动读写位置; \x0d\x0a③ 读写文件内容; \x0d\x0a④ 关闭文件。\x0d\x0a\x0d\x0a七里河团队答疑助人,希望我的回答对你有所帮助

package filewriter  

  

import java.io.FileWriter  

import java.io.IOException  

  

public class IOExceptionDemo {  

  

    private static final String LINE_SEPARATOR = System.getProperty("line.separator")  

    public static void main(String[] args) {  

  

        FileWriter fw = null  

        try {  

            fw = new FileWriter("k:\\Demo.txt", true)  

            fw.write("hello" + LINE_SEPARATOR + "world!")  

        } catch (Exception e) {  

            System.out.println(e.toString())  

        } finally {  

            if (fw != null)  

                try {  

                    fw.close()  

                } catch (IOException e) {  

                    throw new RuntimeException("关闭失败!")  

                }  

        }  

    }  

}

参考java.util.Properties对象进行书写,另外可以在网上找一写辅助书写材料。

代码:

public static void main(String[] args) {

Properties p = new Properties()

p.setProperty("id", "user1")

p.setProperty("password", "123456")

try{

PrintStream stm = new PrintStream(new File("e:\test.properties"))

p.list(stm)

} catch (IOException e) {

e.printStackTrace()

}

}