Java I/O 教程(九) FileWriter和FileReader
FileWriter
Java FileWriter 用於往檔案中寫入字元資料。
不像FileOutputStream類,你無需轉換字串成位元組陣列,因為它提供了直接寫字串的方法。
類定義
public class FileWriter extends OutputStreamWriter
建構函式
FileWriter(File file)
Constructs a FileWriter object given a File object.
|
FileWriter(File file,
boolean append)
Constructs a FileWriter object given a File object.
|
FileWriter(FileDescriptor fd)
Constructs a FileWriter object associated with a file descriptor.
|
FileWriter(String fileName)
Constructs a FileWriter object given a file name.
|
FileWriter(String fileName,
boolean append)
Constructs a FileWriter object given a file name with a boolean indicating whether or not to append the data written.
|
方法
-
Methods inherited from class java.io.OutputStreamWriter
close, flush, getEncoding, write, write, write
請參考:OutputStreamWriter
例子
package com.dylan.io;
import java.io.FileWriter;
/**
* @author xusucheng
* @create 2018-01-07
**/
public class FileWriterExample {
public static void main(String args[]){
try{
FileWriter fw=new FileWriter("D:\\testout.txt");
fw.write("Welcome to java.io.");
fw.close();
}catch(Exception e){System.out.println(e);}
System.out.println("Success...");
}
}
FileReader
Java FileReader 用於從檔案中讀取資料。返回字元資料。
類定義
public class FileReader extends InputStreamReader
建構函式
FileReader(File file)
Creates a new FileReader, given the File to read from.
|
FileReader(FileDescriptor fd)
Creates a new FileReader, given the FileDescriptor to read from.
|
FileReader(String fileName)
Creates a new FileReader, given the name of the file to read from.
|
方法
-
Methods inherited from class java.io.InputStreamReader
close, getEncoding, read, read, ready
具體請參考:InputStreamReader
例子
package com.dylan.io;
import java.io.FileReader;
/**
* @author xusucheng
* @create 2018-01-07
**/
public class FileReaderExample {
public static void main(String args[])throws Exception{
FileReader fr=new FileReader("D:\\testout.txt");
int i;
while((i=fr.read())!=-1)
System.out.print((char)i);
fr.close();
}
}
相關文章
- Java™ 教程(命令列I/O)Java命令列
- Java I/OJava
- 【java】I/O流Java
- Java(8)I/OJava
- Python教程:精簡概述I/O模型與I/O操作Python模型
- 第二十章:非同步和檔案I/O.(九)非同步
- JAVA I/O系統Java
- Java 非同步 I/OJava非同步
- 五種I/O模型和Java NIO原始碼分析模型Java原始碼
- Java中I/O流:阻塞和非阻塞範例Java
- 深入理解Java I/O模型Java模型
- "萬字" Java I/O 詳解Java
- I/O模型、Libuv和Eventloop模型OOP
- Java I/O系統學習系列一:File和RandomAccessFileJavarandomMac
- Java I/O模型及其底層原理Java模型
- 《Java 高階篇》六:I/O 流Java
- 計算機I/O與I/O模型計算機模型
- Java I/O系統學習系列二:輸入和輸出Java
- Java NIO學習系列五:I/O模型Java模型
- I/O流
- 【雜談】Java I/O的底層實現Java
- Java非阻塞I/O模型之NIO說明Java模型
- Netty權威指南:Java的I/O演進NettyJava
- 關於I/O
- c++ I/OC++
- “挑三揀四”地學一學Java I/OJava
- Java I/O流 複製檔案速度對比Java
- 作業系統程式、儲存和I/O作業系統
- 系統級 I/O
- Google I/O Extend 2018Go
- 網路I/O模型模型
- NodeJs 非同步 I/ONodeJS非同步
- 理解I/O Completion Port
- python 非同步 I/OPython非同步
- 02. I/O 操作
- Hadoop的I/O操作Hadoop
- Linux下的5種I/O模型與3組I/O複用Linux模型
- 關於java中的i++和++iJava
- 服務端 I/O 效能:Node、PHP、Java、Go 的對比服務端PHPJavaGo