java 解析文本文件 中换行问题

masterGr 2014-08-25 03:24:01

上图是在编辑器里面显示的要解析文件

此图是在编辑器里点击显示所有字符的结果
怎么用java删除LF
...全文
419 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
masterGr 2014-08-26
  • 打赏
  • 举报
回复
引用 3 楼 xzbang 的回复:


        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");
    
ok可以 太谢谢了
xzbang 2014-08-25
  • 打赏
  • 举报
回复


        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");
    
masterGr 2014-08-25
  • 打赏
  • 举报
回复
引用 1 楼 xzbang 的回复:


		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");
	
我是把excel解析成xml了 12和22行是应该是一行的 只是中间有个字段有几个换行。你说的是加上\r还是有换行的效果 我是想直接把LF删除掉 然后12和22行自然就成一行了
xzbang 2014-08-25
  • 打赏
  • 举报
回复


		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");
	

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧