常用类
1,节点流类型
类型 | 字符流 | 字节流 |
File(文件) |
FileReader FileWriter |
FileInputStream FileOutputStream |
Memory Array |
CharArrayReader CharArrayWriter |
ByteArrayInputStream ByteArrayOutputStream |
Memory String |
StringReader StringWriter |
--- |
Pipe(管道,线程) |
PipedReader PipedWriter |
PipedInputStream PipedOutputStream |
访问文件
FileInputStream和FileOutputStream分别继承自OutputStream和OutputStream,用于向文件中输入和输出字节。
FileInputStream和FileOutputStream的构造方法:
FileInputStream: FileInputStream(File file) Creates a FileInputStream by opening a connection to an actual file, the file named by the File object file in the file system. FileInputStream(FileDescriptor fdObj) Creates a FileInputStream by using the file descriptor fdObj, which represents an existing connection to an actual file in the file system. FileInputStream(String name) Creates a FileInputStream by opening a connection to an actual file, the file named by the path name name in the file system. OutputStream: FileOutputStream(File file) Creates a file output stream to write to the file represented by the specified File object. FileOutputStream(File file, boolean append) Creates a file output stream to write to the file represented by the specified File object. FileOutputStream(FileDescriptor fdObj) Creates a file output stream to write to the specified file descriptor, which represents an existing connection to an actual file in the file system. FileOutputStream(String name) Creates a file output stream to write to the file with the specified name. FileOutputStream(String name, boolean append) Creates a file output stream to write to the file with the specified name.
FileInputStream和FileOutputStream类支持其父类InputStream和OutputStream所提供的数据读写方法。
注意:
在实例化FileInputStream和FileOutputStream时要用ty-catch语句加以处理,因为可能抛出FileNotFoundException。
在读写数据时也要用try-catch语句加以处理,因为可能抛出IOException。
例子:
①,FileInputStream & FileOutputStream
FileInputStream:
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class Test { public static void main(String[] args) { int b = 0; FileInputStream fis = null; try { fis = new FileInputStream("d:\\share\\java\\io\\TestFileInputStream.java"); } catch (FileNotFoundException e) { System.out.println("找不到指定文件"); System.exit(-1); } try { long num = 0; while((b = fis.read()) != -1) { System.out.print((char)b); num++; } fis.close(); System.out.println("共读取了" + num + "个字节"); } catch (IOException e) { System.out.println("文件读取错误"); System.exit(-1); } } }
输出: 对于中文乱码,因为是一个字节一个字节的读取,所以只读取了半个中文
import java.io.*; public class TestFileInputStream { public static void main(String[] args) { int b = 0; FileInputStream in = null; try { in = new FileInputStream("d:\\share\\java\\io\\TestFileInputStream.java"); } catch (FileNotFoundException e) { System.out.println("ÕÒ²»µ½Ö¸¶¨Îļþ"); System.exit(-1); } try { long num = 0; while((b=in.read())!=-1){ System.out.print((char)b); num++; } in.close(); System.out.println(); System.out.println("¹²¶ÁÈ¡ÁË "+num+" ¸ö×Ö½Ú"); } catch (IOException e1) { System.out.println("Îļþ¶ÁÈ¡´íÎó"); System.exit(-1); } } }共读取了700个字节
FileOutputStream
public static void main(String[] args) { int b = 0; FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream("d:/share/java/HelloWorld.java"); fos = new FileOutputStream("d:/share/java/io/HW.java");//没有回自动新建一个文件 while ((b = fis.read()) != -1) { fos.write(b); } fis.close(); fos.close(); } catch (FileNotFoundException e) { System.out.println("找不到指定的文件"); System.exit(-1); } catch (IOException e) { System.out.println("文件复制错误"); System.exit(-1); } System.out.println("文件已复制"); }
②,FileReader & FileWriter
FileReader:
public static void main(String[] args) { int c = 0; FileReader fr = null; try { fr = new FileReader("d:\\share\\java\\io\\TestFileReader.java"); while((c = fr.read()) != -1) { System.out.print((char)c); } fr.close(); } catch (FileNotFoundException e) { System.out.println("找不到指定文件"); } catch (IOException e) { System.out.println("读取文件出错"); } }
FileWriter(一)
public static void main(String[] args) { FileWriter fw = null; try { fw = new FileWriter(new File("d:\\unicode.dat")); for(int i = 0; i <= 50000; i++) {//char最大为65535 fw.write(i); } fw.close(); } catch (IOException e) { e.printStackTrace(); System.out.println("文件写入错误"); System.exit(-1); } }
结果:D盘中的unicode.dat文件中的内容:
FileWriter(二)
public static void main(String[] args) throws Exception { FileReader fr = new FileReader("d:/java/io/TestFileWriter2.java"); FileWriter fw = new FileWriter("d:/java/io/TestFileWriter2.bak"); int b; while((b = fr.read()) != -1) { fw.write(b); } fr.close(); fw.close(); }
时间: 2024-11-08 03:43:31