| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 658 人关注过本帖
标题:怎么反序写文件
只看楼主 加入收藏
bhbh113
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2007-5-22
收藏
 问题点数:0 回复次数:9 
怎么反序写文件
比如文件a.txt中的内容是123,写入b.txt中之后是321,知道的给个思路。
搜索更多相关主题的帖子: 文件 
2007-09-13 16:57
heilong
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:777
专家分:0
注 册:2007-6-7
收藏
得分:0 

把A中看成STRING类型,然后从最后一读取,再写入B中。不知道这样可以不?


风水鸡蛋壳,财去人安乐!----->
2007-09-13 17:08
hwoarangzk
Rank: 4
来 自:冰封王座
等 级:贵宾
威 望:12
帖 子:1894
专家分:0
注 册:2007-7-17
收藏
得分:0 

好像有个方法是倒转String内容的,记不清了,去看看API文档吧


I'm here, as always...
2007-09-13 17:21
purana
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:广东-广州
等 级:版主
威 望:66
帖 子:6039
专家分:0
注 册:2005-6-17
收藏
得分:0 
这是栈的强项.

我的msn: myfend@
2007-09-13 17:30
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 

先把文件里面所有的都读进来放到StringBuilder里面
然后reverse()一下就可以了


可惜不是你,陪我到最后
2007-09-13 18:03
bhbh113
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2007-5-22
收藏
得分:0 
问题是我不知道怎么把一个文件的内容读到一个string中……要是这一步解决了,就没什么问题了。

2007-09-13 19:15
bhbh113
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2007-5-22
收藏
得分:0 

终于把程序写出来了。但是感觉我的算法很糟糕,也没有领会5楼说的采用stringbuilder的方法。厚着脸皮把程序贴出来大家看看吧。欢迎指正。
[CODE]import java.io.*;
public class ReserveCopy {
public ReserveCopy() {
}

public static void main (String[] args)throws IOException {

//创建文件对象
File inputFile=new File("D:\\My Documents\\想飞之心.txt");
File outputFile=new File("D:\\My Documents\\target.txt");

//文件输入和输出流对象
FileReader in=new FileReader(inputFile);
FileWriter out=new FileWriter(outputFile);

String temp="";
String txtStr="";
BufferedReader br=new BufferedReader(in);

//将文件A中的内容读到txtStr中
while(br.readLine()!=null)
{
temp=br.readLine();
txtStr+=temp;
}

char c[]=txtStr.toCharArray();
int i=txtStr.length()-1;

//将A中的内容反序写入B中
while(i>=0)
{
out.write(c[i]);
i--;
}

in.close();
out.close();

}
}[/CODE]


2007-09-13 19:35
george_vcool
Rank: 2
等 级:新手上路
威 望:3
帖 子:453
专家分:0
注 册:2007-7-23
收藏
得分:0 

你这段代码有问题!!

import java.io.*;
public class ReserveCopy {
public ReserveCopy() {
}

public static void main (String[] args)throws IOException {

//创建文件对象
File inputFile=new File("D:\\My Documents\\想飞之心.txt");
File outputFile=new File("D:\\My Documents\\target.txt");

//文件输入和输出流对象
FileReader in=new FileReader(inputFile);
FileWriter out=new FileWriter(outputFile);

String temp="";
String txtStr="";
BufferedReader br=new BufferedReader(in);

//将文件A中的内容读到txtStr中
while(br.readLine()!=null)//改成(temp=br.readLine()) {
temp=br.readLine();//去掉
txtStr+=temp;
}

char c[]=txtStr.toCharArray();
int i=txtStr.length()-1;

//将A中的内容反序写入B中
while(i>=0)
{
out.write(c[i]);
i--;
}

in.close();
out.close();

}
}
2007-09-13 23:55
george_vcool
Rank: 2
等 级:新手上路
威 望:3
帖 子:453
专家分:0
注 册:2007-7-23
收藏
得分:0 
其实你没必要这么麻烦,将txtStr用这个函数reverse()就可以了!
2007-09-13 23:57
phoenixof405
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2007-9-13
收藏
得分:0 

刚查了查JDK
StringBuffer类确实有个类叫做reverse()
public StringBuffer reverse()
The character sequence contained in this string buffer is replaced by the reverse of the sequence.
Let n be the length of the old character sequence, the one contained in the string buffer just prior to execution of the reverse method. Then the character at index k in the new character sequence is equal to the character at index n-k-1 in the old character sequence.

Returns:a reference to this StringBuffer object.

2007-09-14 11:31
快速回复:怎么反序写文件
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011573 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved