求一段简单的java代码
假设有一个a.txt文件,里边的内容为abcde,写一段代码,把a中的内容读出,然后把d替换为f后存储到b.txt文件中(b.txt不存在,需要新建),先行谢过...
假设有一个a.txt文件,里边的内容为abcde,写一段代码,把a中的内容读出,然后把d替换为f后存储到b.txt文件中(b.txt不存在,需要新建),先行谢过
展开
展开全部
不知道是否理解对了你的意思,大概写了一下:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.StringWriter;
public class FileReadAndWrite {
private static final int DEFAULT_BUFFER_SIZE = 1024;
public static void main(String[] args) {
File file = new File("E:/workspace/FileIOTest/src/a.txt");
String str = file2String(file, "UTF-8");
str = str.replace('d', 'f');
string2File(str,"E:/workspace/FileIOTest/src/b.txt");
System.out.println("处理完毕");
}
/**
* 文本文件转换为指定编码的字符串
*
* @param file
* 文本文件
* @param encoding
* 编码类型
* @return 转换后的字符串
* @throws IOException
*/
public static String file2String(File file, String encoding) {
InputStreamReader reader = null;
StringWriter writer = new StringWriter();
try {
if (encoding == null || "".equals(encoding.trim())) {
reader = new InputStreamReader(new FileInputStream(file),
encoding);
} else {
reader = new InputStreamReader(new FileInputStream(file));
}
// 将输入流写入输出流
char[] buffer = new char[DEFAULT_BUFFER_SIZE];
int n = 0;
while (-1 != (n = reader.read(buffer))) {
writer.write(buffer, 0, n);
}
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (reader != null)
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 返回转换结果
if (writer != null)
return writer.toString();
else
return null;
}
/**
* 将字符串写入指定文件(当指定的父路径中文件夹不存在时,会最大限度去创建,以保证保存成功!)
*
* @param res
* 原字符串
* @param filePath
* 文件路径
* @return 成功标记
*/
public static boolean string2File(String res, String filePath) {
boolean flag = true;
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
try {
File distFile = new File(filePath);
if (!distFile.getParentFile().exists())
distFile.getParentFile().mkdirs();
bufferedReader = new BufferedReader(new StringReader(res));
bufferedWriter = new BufferedWriter(new FileWriter(distFile));
char buf[] = new char[1024]; // 字符缓冲区
int len;
while ((len = bufferedReader.read(buf)) != -1) {
bufferedWriter.write(buf, 0, len);
}
bufferedWriter.flush();
bufferedReader.close();
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
flag = false;
return flag;
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return flag;
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.StringWriter;
public class FileReadAndWrite {
private static final int DEFAULT_BUFFER_SIZE = 1024;
public static void main(String[] args) {
File file = new File("E:/workspace/FileIOTest/src/a.txt");
String str = file2String(file, "UTF-8");
str = str.replace('d', 'f');
string2File(str,"E:/workspace/FileIOTest/src/b.txt");
System.out.println("处理完毕");
}
/**
* 文本文件转换为指定编码的字符串
*
* @param file
* 文本文件
* @param encoding
* 编码类型
* @return 转换后的字符串
* @throws IOException
*/
public static String file2String(File file, String encoding) {
InputStreamReader reader = null;
StringWriter writer = new StringWriter();
try {
if (encoding == null || "".equals(encoding.trim())) {
reader = new InputStreamReader(new FileInputStream(file),
encoding);
} else {
reader = new InputStreamReader(new FileInputStream(file));
}
// 将输入流写入输出流
char[] buffer = new char[DEFAULT_BUFFER_SIZE];
int n = 0;
while (-1 != (n = reader.read(buffer))) {
writer.write(buffer, 0, n);
}
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (reader != null)
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 返回转换结果
if (writer != null)
return writer.toString();
else
return null;
}
/**
* 将字符串写入指定文件(当指定的父路径中文件夹不存在时,会最大限度去创建,以保证保存成功!)
*
* @param res
* 原字符串
* @param filePath
* 文件路径
* @return 成功标记
*/
public static boolean string2File(String res, String filePath) {
boolean flag = true;
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
try {
File distFile = new File(filePath);
if (!distFile.getParentFile().exists())
distFile.getParentFile().mkdirs();
bufferedReader = new BufferedReader(new StringReader(res));
bufferedWriter = new BufferedWriter(new FileWriter(distFile));
char buf[] = new char[1024]; // 字符缓冲区
int len;
while ((len = bufferedReader.read(buf)) != -1) {
bufferedWriter.write(buf, 0, len);
}
bufferedWriter.flush();
bufferedReader.close();
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
flag = false;
return flag;
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return flag;
}
}
展开全部
import java.io.*;
public class a {
public static void main(String[] args) {
try {
File f = new File("E:/a.txt"); //a.txt路径
InputStream is = new FileInputStream(f);
BufferedReader bf = new BufferedReader(new InputStreamReader(is));
String st1 = bf.readLine();
String st2 = st1.replace("d", "f");
System.out.println(st2);
File ff = new File("E:/b.txt"); //b.txt路径
OutputStream os = new FileOutputStream(ff);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
bw.write(st2);
bw.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class a {
public static void main(String[] args) {
try {
File f = new File("E:/a.txt"); //a.txt路径
InputStream is = new FileInputStream(f);
BufferedReader bf = new BufferedReader(new InputStreamReader(is));
String st1 = bf.readLine();
String st2 = st1.replace("d", "f");
System.out.println(st2);
File ff = new File("E:/b.txt"); //b.txt路径
OutputStream os = new FileOutputStream(ff);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
bw.write(st2);
bw.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
public class TestTwo {
/**
* 读写文件
* @version Oct 12, 2011 11:01:41 AM
* TODO:
*/
public void readAndWriter(){
try {
File f = new File("D://a.txt");
InputStream in = new FileInputStream(f);
OutputStream op = new FileOutputStream("D://b.txt");
InputStreamReader ir = new InputStreamReader(in);
OutputStreamWriter ow = new OutputStreamWriter(op);
int ch;
StringBuffer buf = new StringBuffer();
while ((ch = ir.read()) > -1) {
char h = (char)ch;
if(h=='d'){
h = 'f';
}
buf.append(h);
}
ow.write(buf.toString());
ow.flush();
ow.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 入口方法
* TODO:
*/
public static void main(String[] args) {
TestTwo t = new TestTwo();
t.readAndWriter();
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
public class TestTwo {
/**
* 读写文件
* @version Oct 12, 2011 11:01:41 AM
* TODO:
*/
public void readAndWriter(){
try {
File f = new File("D://a.txt");
InputStream in = new FileInputStream(f);
OutputStream op = new FileOutputStream("D://b.txt");
InputStreamReader ir = new InputStreamReader(in);
OutputStreamWriter ow = new OutputStreamWriter(op);
int ch;
StringBuffer buf = new StringBuffer();
while ((ch = ir.read()) > -1) {
char h = (char)ch;
if(h=='d'){
h = 'f';
}
buf.append(h);
}
ow.write(buf.toString());
ow.flush();
ow.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 入口方法
* TODO:
*/
public static void main(String[] args) {
TestTwo t = new TestTwo();
t.readAndWriter();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class helloWorld {
public static void main(String[] args){
System.out.println("Hello world Java");
}
}
祝你好运
public static void main(String[] args){
System.out.println("Hello world Java");
}
}
祝你好运
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public static void write(String path, String content) {
String s = new String();
String s1=new String();
String pathstr="a.txt"; //文件绝对路径
File f = new File(pathstr);
BufferedReader input = new BufferedReader(new FileReader(f));
while ((s = input.readLine()) != null) {
s1 += s + "\n";
}
input.close();
s1=s1.replace('d', 'f');
File f2=new File("b.txt");
BufferedWriter output = new BufferedWriter(new FileWriter(f2));
output.write(s1);
output.close();
}
String s = new String();
String s1=new String();
String pathstr="a.txt"; //文件绝对路径
File f = new File(pathstr);
BufferedReader input = new BufferedReader(new FileReader(f));
while ((s = input.readLine()) != null) {
s1 += s + "\n";
}
input.close();
s1=s1.replace('d', 'f');
File f2=new File("b.txt");
BufferedWriter output = new BufferedWriter(new FileWriter(f2));
output.write(s1);
output.close();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询