java读取、修改、写入txt文件
在a.txt文件中保存有若干double类型的数据,读取这些数据,然后排序保存到b.txt文件之中。...
在a.txt文件中保存有若干double类型的数据,读取这些数据,然后排序保存到b.txt文件之中。
展开
9个回答
展开全部
import java.io.*;
/** *//**
*
* 功能描述:创建
TXT
文件并进行读、写、修改操作
*
* @author <a href="mailto:zhanghhui@126.com">KenZhang</a>
* @version 1.0
* Creation date: 2007-12-18 - 下午
06:48:45
*/
public class ReadWriteFile {
public static BufferedReader bufread;
//指定文件路径和名称
private static String path = "D:/suncity.txt";
private static File filename = new File(path);
private static String readStr ="";
/** *//**
* 创建文本文件
.
* @throws IOException
*
*/
public static void creatTxtFile() throws IOException{
if (!filename.exists()) {
filename.createNewFile();
System.err.println(filename + "已创建!
");
}
}
/** *//**
* 读取文本文件
.
*
*/
public static String readTxtFile(){
String read;
FileReader fileread;
try {
fileread = new FileReader(filename);
bufread = new BufferedReader(fileread);
try {
while ((read = bufread.readLine()) != null) {
readStr = readStr + read+ "/r/n";
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("文件内容是
:"+ "/r/n" + readStr);
return readStr;
}
/** *//**
* 写文件
.
*
*/
public static void writeTxtFile(String newStr) throws IOException{
//先读取原有文件内容,然后进行写入操作
String filein = newStr + "/r/n" + readStr + "/r/n";
RandomAccessFile mm = null;
try {
mm = new RandomAccessFile(filename, "rw");
mm.writeBytes(filein);
} catch (IOException e1) {
// TODO 自动生成
catch
块
e1.printStackTrace();
} finally {
if (mm != null) {
try {
mm.close();
} catch (IOException e2) {
// TODO 自动生成
catch
块
e2.printStackTrace();
}
}
}
}
/** *//**
* 将文件中指定内容的第一行替换为其它内容
.
*
* @param oldStr
* 查找内容
* @param replaceStr
* 替换内容
*/
public static void replaceTxtByStr(String oldStr,String replaceStr) {
String temp = "";
try {
File file = new File(path);
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuffer buf = new StringBuffer();
// 保存该行前面的内容
for (int j = 1; (temp = br.readLine()) != null
&& !temp.equals(oldStr); j++) {
buf = buf.append(temp);
buf = buf.append(System.getProperty("line.separator"));
}
// 将内容插入
buf = buf.append(replaceStr);
// 保存该行后面的内容
while ((temp = br.readLine()) != null) {
buf = buf.append(System.getProperty("line.separator"));
buf = buf.append(temp);
}
br.close();
FileOutputStream fos = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(fos);
pw.write(buf.toString().toCharArray());
pw.flush();
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/** *//**
* main方法测试
* @param s
* @throws IOException
*/
public static void main(String[] s) throws IOException {
ReadWriteFile.creatTxtFile();
ReadWriteFile.readTxtFile();
ReadWriteFile.writeTxtFile("20080808:12:13");
// ReadWriteFile.replaceTxtByStr("ken", "zhang");
}
}
/** *//**
*
* 功能描述:创建
TXT
文件并进行读、写、修改操作
*
* @author <a href="mailto:zhanghhui@126.com">KenZhang</a>
* @version 1.0
* Creation date: 2007-12-18 - 下午
06:48:45
*/
public class ReadWriteFile {
public static BufferedReader bufread;
//指定文件路径和名称
private static String path = "D:/suncity.txt";
private static File filename = new File(path);
private static String readStr ="";
/** *//**
* 创建文本文件
.
* @throws IOException
*
*/
public static void creatTxtFile() throws IOException{
if (!filename.exists()) {
filename.createNewFile();
System.err.println(filename + "已创建!
");
}
}
/** *//**
* 读取文本文件
.
*
*/
public static String readTxtFile(){
String read;
FileReader fileread;
try {
fileread = new FileReader(filename);
bufread = new BufferedReader(fileread);
try {
while ((read = bufread.readLine()) != null) {
readStr = readStr + read+ "/r/n";
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("文件内容是
:"+ "/r/n" + readStr);
return readStr;
}
/** *//**
* 写文件
.
*
*/
public static void writeTxtFile(String newStr) throws IOException{
//先读取原有文件内容,然后进行写入操作
String filein = newStr + "/r/n" + readStr + "/r/n";
RandomAccessFile mm = null;
try {
mm = new RandomAccessFile(filename, "rw");
mm.writeBytes(filein);
} catch (IOException e1) {
// TODO 自动生成
catch
块
e1.printStackTrace();
} finally {
if (mm != null) {
try {
mm.close();
} catch (IOException e2) {
// TODO 自动生成
catch
块
e2.printStackTrace();
}
}
}
}
/** *//**
* 将文件中指定内容的第一行替换为其它内容
.
*
* @param oldStr
* 查找内容
* @param replaceStr
* 替换内容
*/
public static void replaceTxtByStr(String oldStr,String replaceStr) {
String temp = "";
try {
File file = new File(path);
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuffer buf = new StringBuffer();
// 保存该行前面的内容
for (int j = 1; (temp = br.readLine()) != null
&& !temp.equals(oldStr); j++) {
buf = buf.append(temp);
buf = buf.append(System.getProperty("line.separator"));
}
// 将内容插入
buf = buf.append(replaceStr);
// 保存该行后面的内容
while ((temp = br.readLine()) != null) {
buf = buf.append(System.getProperty("line.separator"));
buf = buf.append(temp);
}
br.close();
FileOutputStream fos = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(fos);
pw.write(buf.toString().toCharArray());
pw.flush();
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/** *//**
* main方法测试
* @param s
* @throws IOException
*/
public static void main(String[] s) throws IOException {
ReadWriteFile.creatTxtFile();
ReadWriteFile.readTxtFile();
ReadWriteFile.writeTxtFile("20080808:12:13");
// ReadWriteFile.replaceTxtByStr("ken", "zhang");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public static void main(String[] args) {
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
try {
bufferedReader = new BufferedReader(new FileReader(new File("a.txt")));
bufferedWriter = new BufferedWriter(new FileWriter(new File("b.txt")));
String lineStr = "";
while((lineStr = bufferedReader.readLine()) != null){
bufferedWriter.write(lineStr);
bufferedWriter.newLine();
bufferedWriter.flush();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(bufferedWriter != null){
try {
bufferedWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(bufferedReader != null){
try {
bufferedReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
try {
bufferedReader = new BufferedReader(new FileReader(new File("a.txt")));
bufferedWriter = new BufferedWriter(new FileWriter(new File("b.txt")));
String lineStr = "";
while((lineStr = bufferedReader.readLine()) != null){
bufferedWriter.write(lineStr);
bufferedWriter.newLine();
bufferedWriter.flush();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(bufferedWriter != null){
try {
bufferedWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(bufferedReader != null){
try {
bufferedReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用数据流读取a.txt文件中的数据
将数据解析排序
将数据写入到b.txt文件中
大概就这个思路 具体实现请自行bdidu!
将数据解析排序
将数据写入到b.txt文件中
大概就这个思路 具体实现请自行bdidu!
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
一行一行放入,一行一行读再用Double.parseDouble()方法解析
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询