在JAVA中如何①利用字符流读写一个指定文本文件。②利用读写器类实现对指定文本的读写。
2个回答
2011-11-14
展开全部
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();
}
}
}
}
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();
}
}
}
}
展开全部
用字符流读取文件: URL指的是文件路径
BufferedReader read=new BufferedReader(new FileReader(url));
read.readLine() 按行读取文本文件
写文件: URL指的是文件路径
BufferedWriter writer=new BufferedWriter(new FileWriter(url),true);
writer.append(str) 向指定文件追加内容,str指要追加的内容
writer.flush(); 记得最后刷新,把Writer的内容写到文件中
BufferedReader read=new BufferedReader(new FileReader(url));
read.readLine() 按行读取文本文件
写文件: URL指的是文件路径
BufferedWriter writer=new BufferedWriter(new FileWriter(url),true);
writer.append(str) 向指定文件追加内容,str指要追加的内容
writer.flush(); 记得最后刷新,把Writer的内容写到文件中
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询