黑马程序员技术交流社区
标题:
文档的copy乱码了 各位帮看看
[打印本页]
作者:
宗士为
时间:
2012-5-14 08:43
标题:
文档的copy乱码了 各位帮看看
//2.将day19-笔记.txt中的所有行反转(第一行换到最后一行, 第二行换到倒数第二行)
public class Exercise2 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("day19-笔记.txt"));
List<String> list = new ArrayList<String>();
String line;
while((line = br.readLine()) != null)
list.add(line);
br.close();
BufferedWriter w = new BufferedWriter(new FileWriter("invertday19.txt"));
for(int i = list.size() - 1; i >= 0; i--)
w.write(i);
w.close();
}
}
"day19-笔记.txt"是个文档文件 ,通过要求我写了代码 但是编译过后invertday19.txt出现了乱码
检查了10分种 也没看出问题 大哥大姐帮看看
作者:
郭振
时间:
2012-5-14 09:31
兄弟,我没有按照你的ArrayList做的,我用LinkList做的。
import java.io.*;
import java.util.*;
public class Exercise2 {
public static void main(String[] args) throws IOException {
File f=new File("e:\\a.txt");
if(!f.exists())
f.createNewFile();
BufferedReader br = new BufferedReader(new FileReader(f));
LinkedList<String> list = new LinkedList<String>();
String line=null;
while((line = br.readLine()) != null)
list.addFirst(line);
br.close();
BufferedWriter w = new BufferedWriter(new FileWriter("e:\\b.txt"));
/* for(int i = list.size() - 1; i >= 0; i--)
w.write(i);*/
for(String s:list)
{
w.write(s);
w.newLine();
}
w.close();
}
}
作者:
赵玮_Tom
时间:
2012-5-14 09:49
本帖最后由 赵玮_Tom 于 2012-5-14 09:51 编辑
程序已经修改完毕,并进行了标注:(流关联的文件已经做了改动,但不影响程序执行)
import java.io.*;
import java.util.*;
//2.将day19-笔记.txt中的所有行反转(第一行换到最后一行, 第二行换到倒数第二行)
public class Exercise2
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new FileReader("Exercise2.java"));
List<String> list = new ArrayList<String>();
String line;
while((line = br.readLine()) != null)
list.add(line);
br.close();
BufferedWriter w = new BufferedWriter(new FileWriter("dddddd.txt"));
/*
//此处已经把集合和数组的操作弄混!!!
for(int i = list.size() - 1; i >= 0; i--)
w.write(i);
w.close();
*/
Object [] strs = list.toArray
();//将集合转为数组!
for(int i=strs.length-1; i>=0; i--)
{
w.write((String)strs
);
//Object类型转为String
w.newLine();
//必须换行
}
w.close();
}
}
原程序的主要问题在于将
集合和数组的操作弄混!
作者:
蒋亮
时间:
2012-5-14 11:16
import java.util.*;
import java.io.*;
class InvertFile
{
public static void main(String []args) throws IOException
{
File file=new File("Peaches.java");
invert(file);
}
private static void invert(File file)throws IOException
{
BufferedReader br=new BufferedReader(new FileReader(file));
List<String> list=new ArrayList<String>();
String line=null;
int j=1;
while((line=br.readLine())!=null)
{
System.out.println(j+++":"+line);
list.add(line);
}
br.close();
System.out.println(list.size());
BufferedWriter bw=new BufferedWriter(new FileWriter("invertPeaches.java"));
PrintWriter pw=new PrintWriter(bw,true);
for(int i=list.size()-1;i>=0;i--)
{
if(i==0)
{
pw.print(list.get(i));
}
else
pw.println(list.get(i));
}
pw.close();
}
}
复制代码
你程序中的主要错误是在循环中写入的不是集合中存放的内容,你用list.get(i)取出i位置的内容再写入新的文件中就可以了,写入一行文件用PrintWriter来装饰BufferedWriter更方便,楼上两位兄弟的代码在执行后会在新的文件中多出一行空行,与楼主要求不合。另外2楼说将集合和数组的操作弄混不怎么恰当,集合本身就是一个动态数组。另外用LinkedList效率比ArrayList要高,但就楼主这道题而言无所谓,因为不会牵涉到数组元素的移动。。。还不懂的话加Q聊,282713672
作者:
小小企鹅
时间:
2012-5-14 11:37
w.write(list.get(i));
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2