Java 如何将多行文件转换为以控制字符分隔的单行文件

Java 如何将多行文件转换为以控制字符分隔的单行文件,java,regex,eclipse,parsing,stringbuilder,Java,Regex,Eclipse,Parsing,Stringbuilder,我正在尝试解析如下文件: _=1406048396605<CONTROL_CHARACTER_HERE>bh=1244<CONTROL_CHARACTER_HERE>bw=1711<CONTROL_CHARACTER_HERE>c=24<CONTROL_CHARACTER_HERE>c19=DashboardScreenc2=2014-07-22T10:00:00-0700.....etc textfile.txt _=1406048396605

我正在尝试解析如下文件:

_=1406048396605<CONTROL_CHARACTER_HERE>bh=1244<CONTROL_CHARACTER_HERE>bw=1711<CONTROL_CHARACTER_HERE>c=24<CONTROL_CHARACTER_HERE>c19=DashboardScreenc2=2014-07-22T10:00:00-0700.....etc
textfile.txt

_=1406048396605
bh=1244
bw=1711
c=24
c19=DashboardScreen
c2=2014-07-22T10:00:00-0700
c4=64144090210294
c40=3#undefined#0#0#a=-2#512#-1#0
c41=14060470498427c3e4ed
c46=Green|firefox|Firefox|30|macx|Mac OS X
c5=NONFFA
c6=HGKhjgj
c7=OFF_SEASON|h:PARTIAL|
ch=YHtgsfT
g=https://google.hello.com
h5=77dbf90c-5794-4a40-b1ab-fe1c82440c68-1406048401346
k=true
p=Shockwave Flash;QuickTime Plug-in 7.7.3;Default Browser Helper;SharePoint Browser Plug-in;Java Applet Plug-in;Silverlight Plug-In
pageName=DashboardScreen - Loading...
pageType= 
pe=lnk_o
pev2=pageDetail
s=2432x1520
server=1.1 pqalmttws301.ie.google.net:81
t=22/06/2014 10:00:00 2 420
v12=3468337910
v4=0
v9=dat=279333:279364:375870:743798:744035:743802:744033:743805:783950:783797:783949:784088
vid=29E364C5051D2894-400001468000F0EE
变成这样:

_=1406048396605<CONTROL_CHARACTER_HERE>bh=1244<CONTROL_CHARACTER_HERE>bw=1711<CONTROL_CHARACTER_HERE>c=24<CONTROL_CHARACTER_HERE>c19=DashboardScreenc2=2014-07-22T10:00:00-0700.....etc

我甚至不确定我是否做对了。有人知道怎么做吗

因为它是一个文本文件,所以您必须使用类来读取字符流。为了获得更好的性能,请使用

从字符输入流读取文本,缓冲字符,以便高效读取字符、数组和行

你可以用

示例代码:

try (BufferedReader reader = new BufferedReader(new FileReader(
        new File("InputFile.txt")));
     BufferedWriter writer = new BufferedWriter(new FileWriter(
        new File("OutputFile.txt")))) {
    String line = null;
    while ((line = reader.readLine()) != null) {
        writer.write(line);
        // write you <CONTROL_CHARACTER_HERE> as well
    }
}
try(BufferedReader=new BufferedReader(new FileReader(
新文件(“InputFile.txt”);
BufferedWriter writer=新的BufferedWriter(新文件编写器(
新文件(“OutputFile.txt”)){
字符串行=null;
而((line=reader.readLine())!=null){
作者:写(行);
//也给你写信
}
}

因为它是一个文本文件,所以您必须使用类来读取字符流。为了获得更好的性能,请使用

从字符输入流读取文本,缓冲字符,以便高效读取字符、数组和行

你可以用

示例代码:

try (BufferedReader reader = new BufferedReader(new FileReader(
        new File("InputFile.txt")));
     BufferedWriter writer = new BufferedWriter(new FileWriter(
        new File("OutputFile.txt")))) {
    String line = null;
    while ((line = reader.readLine()) != null) {
        writer.write(line);
        // write you <CONTROL_CHARACTER_HERE> as well
    }
}
try(BufferedReader=new BufferedReader(new FileReader(
新文件(“InputFile.txt”);
BufferedWriter writer=新的BufferedWriter(新文件编写器(
新文件(“OutputFile.txt”)){
字符串行=null;
而((line=reader.readLine())!=null){
作者:写(行);
//也给你写信
}
}

最简单的方法是使用
扫描仪
打印机

    Scanner in = null;
    PrintWriter out = null;
    try {
        // init input, output
        in = new Scanner(new File("InputFile.txt"));
        out = new PrintWriter(new File("OutputFile.txt"));
        // read input file line by line
        while (in.hasNextLine()) {
            out.print(in.nextLine());
            if (in.hasNextLine()) {
                out.print("<CONTROL_CHARACTER>");
            }
        }
    } finally {
        // close input, output
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    }
Scanner in=null;
PrintWriter out=null;
试一试{
//初始化输入,输出
in=新扫描仪(新文件(“InputFile.txt”);
out=新的PrintWriter(新文件(“OutputFile.txt”);
//逐行读取输入文件
while(在.hasNextLine()中){
out.print(in.nextLine());
if(在.hasNextLine()中){
输出。打印(“”);
}
}
}最后{
//关闭输入、输出
if(in!=null){
in.close();
}
if(out!=null){
out.close();
}
}

最简单的方法是使用
扫描仪
打印机

    Scanner in = null;
    PrintWriter out = null;
    try {
        // init input, output
        in = new Scanner(new File("InputFile.txt"));
        out = new PrintWriter(new File("OutputFile.txt"));
        // read input file line by line
        while (in.hasNextLine()) {
            out.print(in.nextLine());
            if (in.hasNextLine()) {
                out.print("<CONTROL_CHARACTER>");
            }
        }
    } finally {
        // close input, output
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    }
Scanner in=null;
PrintWriter out=null;
试一试{
//初始化输入,输出
in=新扫描仪(新文件(“InputFile.txt”);
out=新的PrintWriter(新文件(“OutputFile.txt”);
//逐行读取输入文件
while(在.hasNextLine()中){
out.print(in.nextLine());
if(在.hasNextLine()中){
输出。打印(“”);
}
}
}最后{
//关闭输入、输出
if(in!=null){
in.close();
}
if(out!=null){
out.close();
}
}

以下三段代码将读取文件,用您的
替换所有换行符,然后写入文件

读取文件:

public static String readFile(String filePath) {
    String entireFile = "";

    File file = new File(filePath);

    if (file.exists()) {
        BufferedReader br;
        try {
            br = new BufferedReader(new FileReader(file));

            String line;
            while ((line = br.readLine()) != null) {
                entireFile += line + "\n";
            }

            br.close();

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else {
        System.err.println("File " + filePath + " does not exist!");
    }

    return entireFile;
}
writeToFile("Path/to/newfile.txt", text);
将换行符更改为

下面是方法
writeToFile()


下面是三段代码,它们将读取文件,用
替换所有换行符,然后写入文件

读取文件:

public static String readFile(String filePath) {
    String entireFile = "";

    File file = new File(filePath);

    if (file.exists()) {
        BufferedReader br;
        try {
            br = new BufferedReader(new FileReader(file));

            String line;
            while ((line = br.readLine()) != null) {
                entireFile += line + "\n";
            }

            br.close();

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else {
        System.err.println("File " + filePath + " does not exist!");
    }

    return entireFile;
}
writeToFile("Path/to/newfile.txt", text);
将换行符更改为

下面是方法
writeToFile()

  • 用过
  • 适用于小文件。未测试大文件和非常大的文件。我认为根据问题,预期输入文件的大小较小
  • 这里我们不处理每一行,所以不需要逐行阅读
使用Guava IO库的另一种方法
publicstaticvoidmain(字符串[]args){
试一试{
String content=Files.toString(新文件(“/home/chandrayya/InputFile.txt”),Charsets.UTF_8);//相应地更改字符集
content=content.replaceAll(“\r\n”/*\r\n windows格式,\n UNIX/OSX格式\r旧mac格式*/,”/*C是控制字符。*/);
write(内容,新文件(“/home/chandrayya/OutputFile.txt.txt”)、Charsets.UTF_8;
}捕获(IOE异常){
e、 printStackTrace();
}
}
  • 用过
  • 适用于小文件。未测试大文件和非常大的文件。我认为根据问题,预期输入文件的大小较小
  • 这里我们不处理每一行,所以不需要逐行阅读
使用Guava IO库的另一种方法
publicstaticvoidmain(字符串[]args){
试一试{
String content=Files.toString(新文件(“/home/chandrayya/InputFile.txt”),Charsets.UTF_8);//相应地更改字符集
content=content.replaceAll(“\r\n”/*\r\n windows格式,\n UNIX/OSX格式\r旧mac格式*/,”/*C是控制字符。*/);
write(内容,新文件(“/home/chandrayya/OutputFile.txt.txt”)、Charsets.UTF_8;
}捕获(IOE异常){
e、 printStackTrace();
}
}

您的问题是什么?为什么不逐行读取文件,并在每行之前(第一行除外)将这些行写入另一个文件中?@Pshemo我该怎么做Pshemo?我是Java新手,你有什么问题?为什么不逐行读取文件,并在每行之前使用
将这些行写入另一个文件中,第一行除外?@Pshemo我怎么能这样做?我对Java还不熟悉,但我不想删除我的感谢。你的评论是有效的,没有必要删除它,因为正如我在上一篇评论中提到的,我已经编辑了它。但我不想删除我的感谢。您的评论是有效的,没有必要删除它,因为正如我在上一篇评论中提到的,我已经编辑了它。