java用字符流读写文件

java用字符流读写文件,第1张

代码已写好,希望对你有帮助,顺便求好评。

package architecture

import java.io.File

import java.io.FileNotFoundException

import java.io.FileReader

import java.io.FileWriter

import java.io.IOException

/**

 * 

 * @author Wang Zhenhui

 * @blog www.soaringroad.com

 *

 */

public class AboutIOReaderWriter {

/**

 * main

 * @param args

 */

public static void main(String[] args) {

String filePath = "d:\\out2.txt"

write(filePath)

read(filePath)

}

/**

 * Read

 * @param filePath file path

 */

public static void read(String filePath) {

FileReader fr = null

StringBuilder result = new StringBuilder()

try {

fr = new FileReader(new File(filePath))

char[] charArray = new char[1024]

while (fr.read(charArray) != -1) {

result.append(charArray)

}

} catch (FileNotFoundException e) {

e.printStackTrace()

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace()

}

if (fr != null) {

try {

fr.close()

} catch (IOException e) {

e.printStackTrace()

}

}

System.out.println(result.toString())

}

/**

 * Write

 * @param filePath file path

 */

public static void write(String filePath) {

FileWriter fw = null

try {

fw = new FileWriter(new File(filePath))

for (int i = 1 i <= 50 i++) {

fw.append(String.valueOf(i))

}

fw.flush()

} catch (IOException e) {

e.printStackTrace()

}

if (fw != null) {

try {

fw.close()

} catch (IOException e) {

e.printStackTrace()

}

}

}

}

字符流的特点是:

字符流是为了方便我们读写文本文件,在换句话说,字符流只能读写文本文件,其他类型的文件读写不了。

OutputStreamWriter是字符流通向字节流的桥梁:可使用指定的charset将要写入流中的字符编码成字节。它使用的字符集可以由名称指定或显式给定,否则将接受平台默认的字符集。

import java.io.BufferedReader

import java.io.FileInputStream

import java.io.FileOutputStream

import java.io.IOException

import java.io.InputStreamReader

import java.io.OutputStreamWriter

public class ReaderAndWriter {

/**

*

* @param path 完整路径 如 c:\test.txt

* @param charset 字符集,若为null采用平台默认字符集

* @throws IOException

*/

public void read(String path, String charset) throws IOException {

BufferedReader reader=null

InputStreamReader isr=null

try {

if(null!=charset){

isr=new InputStreamReader(new FileInputStream(path),charset)

}else{

isr=new InputStreamReader(new FileInputStream(path))

}

reader=new BufferedReader(isr)

String tmp=reader.readLine()

while(null!=tmp){

System.out.println(tmp)

tmp=reader.readLine()

}

} finally{

if(null!=reader){

reader.close()

}

}

}

/**

*

* @param path 文件绝对路径 如 c:\test.txt,若文件存在,则覆盖内容

* @param content 要写入文件的内容

* @param charset 字符集,若为null,则采用平台默认字符集

* @throws IOException

*/

public void write(String path, String content, String charset)

throws IOException {

FileOutputStream fos = new FileOutputStream(path)

OutputStreamWriter writer = null

try {

if (null != charset) {

writer = new OutputStreamWriter(fos, charset)

} else {

writer = new OutputStreamWriter(fos)

}

writer.append(content)

} finally {

if (null != writer) {

writer.flush()

writer.close()

}

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/12098353.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存