String filePath = "E:\\testtesttest\\test2.txt"; BufferedReader s = null; try {
String str = "";
s = new BufferedReader(new InputStreamReader(new FileInputStream(
new File(filePath)), "GB2312"));
PrintWriter pw = new PrintWriter(new File("E:\\testtesttest\\test3.txt"),"GB2312");
while ((str = s.readLine()) != null) {
pw.print(str+"\r");//不写入“\n”
pw.flush();
}
pw.close();
} catch (FileNotFoundException e) {
System.out.println("找不到指定文件");
} catch (IOException e) {
System.out.println("读取文件失败");
} finally {
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}

System.out.println("over");

解决方案 »

  1.   

    我是把excel解析成xml了 12和22行是应该是一行的 只是中间有个字段有几个换行。你说的是加上\r还是有换行的效果 我是想直接把LF删除掉 然后12和22行自然就成一行了
      

  2.   

            String result = "";
            File file = new File("E:\\testtesttest\\test2.txt");
            InputStream in = null;
            byte[] tempByte = new byte[1024];
            int byteread = 0;
           
            try {
                in = new FileInputStream(file);
                while ((byteread = in.read(tempByte)) != -1 ) {
                    result += new String(tempByte, 0, byteread);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            
            result = result.replaceAll("\n", "");
            
            try{
             PrintWriter pw = new PrintWriter(new File("E:\\testtesttest\\test3.txt"));
             pw.print(result);
             pw.flush();
             pw.close();
            }catch(Exception e){
             e.printStackTrace();
            }
            System.out.println("over");