我有一个由竖线分隔的文本文件。 我想创建一个新的文本文件(逗号分隔)。 我下面的内容只在文本文件中写了一行! 但System.Out.PrintIn将所有行打印到控制台!
这是我的代码。
private void doImport(String sfile,String outfile, String gl, int period, String glacc,String contral) throws FileNotFoundException
{
//java.util.List<Exports> pp= new ArrayList<Exports>();
// imported data date format
SimpleDateFormat fm =new SimpleDateFormat("dd-MMM-yy");
// output file date format
SimpleDateFormat fm1 =new SimpleDateFormat("dd-MM-yyyy");
// file with data i want to change to new format
File file = new File(sfile);
// if file null?...add code in calling method to exit
// first line headers...counter to track that
int count=0;
FileReader f= new FileReader(file);
BufferedReader r = new BufferedReader(f);
String l =null;
try {
while((l=r.readLine()) != null)
{
count=count+1;
// skip first line
if (count>1)
{
// split data on | character
String tokenizer="\\|";
String[] s= l.split(tokenizer);
// import only for references starting with f
if(s[0].trim().startsWith("F"))
{
Date d= null;
d= fm.parse(s[6].trim());
Exports p= new Exports();
p.setPeriod(period);
p.setExchangerate(1);
p.setReference(s[0].trim());
p.setGlcode(gl);
p.setDate(fm1.format(d));
p.setDescription(s[2].trim());
p.setGlaccount(glacc);
p.setContral(contral);
p.setEmpty1(" ");
p.setEmpty2(" ");
p.setAmount(Double.parseDouble(s[9].trim()));
p.setHomeamount(Double.parseDouble(s[9].trim()));
p.setZero1(0);
p.setZero2(0);
p.setZ1(0);
p.setZ2(0);
p.setZ3(0);
p.setOne(1);
p.setEftnumber((s[5].trim()));
try
{
File f1 = new File(outfile);
if (f1.exists()) {
// kill
f1.delete();
// create it then!
f1.createNewFile();
}
FileWriter writer= new FileWriter(f1);
BufferedWriter b= new BufferedWriter(writer);
b.write(p.toString());
b.newLine();
b.close();
}catch(Exception e)
{
e.printStackTrace();
}
System.out.println(p.toString());
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
为什么我只添加了一行?
罗纳德
答案 0 :(得分:2)
您必须在循环之前创建一次输出文件。不在循环中。
private void doImport(String sfile,String outfile, String gl, int period, String glacc,String contral) throws FileNotFoundException
{
//java.util.List<Exports> pp= new ArrayList<Exports>();
// imported data date format
SimpleDateFormat fm =new SimpleDateFormat("dd-MMM-yy");
// output file date format
SimpleDateFormat fm1 =new SimpleDateFormat("dd-MM-yyyy");
// file with data i want to change to new format
File file = new File(sfile);
// if file null?...add code in calling method to exit
// first line headers...counter to track that
int count=0;
FileReader f= new FileReader(file);
BufferedReader r = new BufferedReader(f);
String l =null;
try {
File f1 = new File(outfile);
if (f1.exists()) {
// kill
f1.delete();
// create it then!
f1.createNewFile();
}
while((l=r.readLine()) != null)
{
count=count+1;
// skip first line
if (count>1)
{
// split data on | character
String tokenizer="\\|";
String[] s= l.split(tokenizer);
// import only for references starting with f
if(s[0].trim().startsWith("F"))
{
Date d= null;
d= fm.parse(s[6].trim());
Exports p= new Exports();
p.setPeriod(period);
p.setExchangerate(1);
p.setReference(s[0].trim());
p.setGlcode(gl);
p.setDate(fm1.format(d));
p.setDescription(s[2].trim());
p.setGlaccount(glacc);
p.setContral(contral);
p.setEmpty1(" ");
p.setEmpty2(" ");
p.setAmount(Double.parseDouble(s[9].trim()));
p.setHomeamount(Double.parseDouble(s[9].trim()));
p.setZero1(0);
p.setZero2(0);
p.setZ1(0);
p.setZ2(0);
p.setZ3(0);
p.setOne(1);
p.setEftnumber((s[5].trim()));
FileWriter writer= new FileWriter(f1);
BufferedWriter b= new BufferedWriter(writer);
b.write(p.toString());
b.newLine();
b.close();
}catch(Exception e)
{
e.printStackTrace();
}
System.out.println(p.toString());
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
答案 1 :(得分:1)
在此代码中
{
File f1 = new File(outfile);
if (f1.exists()) {
// kill
f1.delete();
// create it then!
f1.createNewFile();
}
FileWriter writer= new FileWriter(f1);
BufferedWriter b= new BufferedWriter(writer);
b.write(p.toString());
b.newLine();
b.close();
}
您正在删除为循环的每次迭代创建一个新文件。
您可以在输入文件和Stream
的同时创建输出文件和Stream File file = new File(sfile);
FileReader f= new FileReader(file);
BufferedReader r = new BufferedReader(f);
File f1 = new File(outfile);
if (f1.exists()) {
// kill
f1.delete();
// create it then!
f1.createNewFile();
}
FileWriter writer= new FileWriter(f1);
BufferedWriter b= new BufferedWriter(writer);