当前位置:编程学习 > JAVA >>

请高手帮我看一下,为什么不能写入到文件中去,有劳了

package com.dell.tool;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.Calendar;

public class OmuTool {
@SuppressWarnings("deprecation")
public static void main(String[] args) throws IOException {
readFile();
}

private static void readFile() {
try {
File fin = new File("D:\\Program Files\\OMU_Tool\\result.txt");
BufferedReader br = new BufferedReader(new FileReader(fin));
File fout = new File("D:\\spit_result.txt");
if (fout.exists())
fout.delete();
else
fout.createNewFile();
FileWriter fw = new FileWriter(fout);
BufferedWriter bw = new BufferedWriter(fw);
String str = null;
do {
str=br.readLine();
str=deleteComments(str);
if(str.length()==0)continue;
bw.write(str);
bw.newLine();
System.out.println(str.trim());
} while (str != null);
bw.flush();
br.close();
bw.close();
} catch (Exception e) {
System.err.println("File not found!");
}
}

static String deleteComments(String str) {
if (str == null)
return str;
int zos = str.indexOf("框");
if (zos < 0)
return str;
return str.substring(0, zos - 3);
}
}
--------------------编程问答-------------------- ==================================================
IP: 16.165.15.131
产品类型: aBSC
语言类型: Chinese
工作区  =  sion_a
主用工作区版本  =  B900R414C00A010
备用工作区版本  =  B900R914C00A909
 0 框  aaaaaaaaaaaaaaaaa
 0 框  aaaaaaaaaaaaa
 0 框  aaaaaaaaaaaaaaaa
查询OK
==================================================
IP: 16.165.15.131
产品类型: aBSC
语言类型: Chinese
工作区  =  sion_a
主用工作区版本  =  B900R414C00A010
备用工作区版本  =  B900R914C00A909
 0 框  aaaaaaaaaaaaaaaaa
 0 框  aaaaaaaaaaaaa
 0 框  aaaaaaaaaaaaaaaa
查询OK --------------------编程问答-------------------- 上面是result.txt的内容 --------------------编程问答--------------------

while ((str = br.readLine()) != null) {
str = deleteComments(str);
if (str.length() == 0)
continue;
bw.write(str);
bw.newLine();
System.out.println(str.trim());
} ;
--------------------编程问答-------------------- 可以debug一下,看下执行顺序 --------------------编程问答-------------------- 1
if (str.length() == 0)
改成:
if (str == null || str.length() == 0)

2 不要catch异常后打一个不能反映原来面貌的log
catch (Exception e)
{
e.printStackTrace();
}

另外,楼主看起来在做日志分析,linux下可以用grep就可以了

grep -V "框" result.txt > split_result.txt --------------------编程问答--------------------

package com.dell.tool;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.Calendar;

public class OmuTool{
@SuppressWarnings("deprecation")
public static void main(String[] args) throws IOException {
readFile();
}

private static void readFile() {
try {
File fin = new File("D:\\result.txt");
BufferedReader br = new BufferedReader(new FileReader(fin));
File fout = new File("D:\\spit_result.txt");
if (fout.exists())
fout.delete();
else
fout.createNewFile();
FileWriter fw = new FileWriter(fout);
BufferedWriter bw = new BufferedWriter(fw);
String str = null;
do {
str=br.readLine();
str=deleteComments(str);
if(str.length()==0)continue;
bw.write(str);
bw.newLine(); 
System.out.println(str.trim());
} while (str != null);
bw.flush();
br.close();
bw.close();


catch (NullPointerException e) { //空指针异常,调用str.length()引起的,楼上们正解,我也学习一下
System.err.println("空指针异常!");
}
catch (Exception e) {
System.err.println("File not found!");
}
}

static String deleteComments(String str) {
if (str == null)
return str;
int zos = str.indexOf("框");
if (zos < 0)
return str;
return str.substring(0, zos - 3);
}
}

补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,