将输出打印到文件 [英] print output to a file
本文介绍了将输出打印到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我在此代码中的代码,可以,但是我无法将输出打印到文件中.您能帮我看看错误在哪里吗
This is my code in this i m gettng o/p ,evrythng if fine but i was unable to print output to a file.Can u help me where was the mistake actually
import java.io.*;
import java.util.*;
public class MainClass {
public static void main(String[] args) throws Exception {
int flag=0,cindex=0;;
String fname=null,first=null,second=null;
TreeMap tm=new TreeMap();
File f=new File("D:\\final.txt");
f.createNewFile();
PrintWriter pw=new PrintWriter(new FileWriter(f));
File f1=new File("D:\\trnstons");
String line=null;
Map hm2;
Set s2;
String lkeys=null,lvalues=null;
int c=0;
//BufferedReader br1;
f1.mkdir();
String s[]=f1.list();
//System.out.println("TOTAL COUNT OF FILE::"+s.length);
int fcount=s.length;
for(String s1:s)
{
File f2=new File("D:\\trnstons\\"+s1);
BufferedReader br1=new BufferedReader(new FileReader(f2));
System.out.println( " "+f2.getName());
//pw.println("===========================FILE NAME IS::====================="+f2.getName());
line=br1.readLine();
while(line!=null)
{
while((line.equals("SUCCESS")))
{
line=br1.readLine();
while(line!=null)
{
//System.out.println(line);
cindex=line.indexOf(",");
if(cindex > (-1))
{
first=line.substring(0,cindex);
second=line.substring(cindex+1);
tm.put(second,first);
}
//pw.print(line);
line=br1.readLine();
}
////
flag=1;
break;
}
if(flag==1)
break;
line=br1.readLine();
}
/////
//System.out.println(tm);
}
//the values in tm are sorted according to values.Now for printing in file as k,v pairs we need to conver to LinkedHashMap
hm2=new LinkedHashMap(tm);
s2=hm2.entrySet();
Iterator i=s2.iterator();
//System.out.println("the values in the tm object are");
while(i.hasNext())
{
Map.Entry me=(Map.Entry)i.next();
lkeys= (String)me.getKey();/*as the getKey and GetValue returns key and value in the String form,convert in to ArrayList
(it implements RandonAccess(I),so its good at retriveing rather that any other List */
lvalues= (String)me.getValue();
System.out.print(lvalues);
System.out.print(",");
System.out.println(lkeys);
///trying to print in file
pw.print(lvalues);
pw.print(",");
pw.println(lkeys);
}
}
}
推荐答案
Java不是我的强项,但我认为您在这里需要额外的\或更少的\ ..
Java isn''t my forte, but I think you either need an extra \ or one less \ here..
or(String s1:s)
{
File f2=new File("D:\\trnstons\\"+s1);
(具体来说,最后一个双\\)
(Specifically, the last double \\)
我要说的是,您应该在某一时刻刷新您的编写器并关闭文件编写器...
还要将FileWriter声明为变量:
I would say, that you should at one point flsh your writer and close the filewriter...
Also declare the FileWriter as variable:
// old: PrintWriter pw=new PrintWriter(new FileWriter(f));
FileWriter fw = new FileWriter(f);
PrintWriter pw=new PrintWriter(fw);
// ...
try {
pw.flush();
pw.close();
fw.flush();
fw.close();
} catch (IOException e) {
System.err.println("Error writing to file:" + e.getMessage());
}
完成对任何文件的写入后,应使用任何文件进行此操作.要在结束写入之前获取未完成的内容,只需使用flush()-Method将内容获取到磁盘...
希望这会有所帮助,
干杯,阿恩特
This should be done with any file, when you finished writing to it. To get unfinished content before ending the write, just use the flush()-Method to get content to Disk...
Hope this helps,
Cheers, Arndt
这篇关于将输出打印到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文