JAVA我在做什么错,我要线 [英] JAVA What am I doing wrong, I want the line

查看:100
本文介绍了JAVA我在做什么错,我要线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试进行更改日志,因此我需要在某些句子之间添加一行. 我所拥有的只是这个,但似乎没有用.有人可以帮我吗?

I am trying to make a change log and so I need a single line between some sentences. All I have is this but it doesn't seem to work. Can anyone help me please?

@Test
public void addLine() {
    File temp;
    try {
        temp = File.createTempFile("app.log", ".tmp", new File("."));
        File appLog = new File("app.log");
        try (BufferedWriter bw = new BufferedWriter(new FileWriter(temp));
                BufferedReader br = new BufferedReader(new FileReader(
                        appLog))) {
            String line;
            while ((line = br.readLine()) != null) {
                bw.write(line);
                bw.newLine();
                if ("2 A".equals(line)) {
                    bw.write("New Line!");
                    bw.newLine();
                }
            }
            appLog.delete();
            temp.renameTo(appLog);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

推荐答案

您可能遇到的问题可能是由于BufferedWriter使用的行分隔符"(在创建所述类时会设置它).我认为最好改用:

The problem that you might be encountering might be because of the "line separator" used by the BufferedWriter (it gets set when you create said class). I think it would be best to use instead:

System.getProperty("line.separator");

通过这种方式,您可以使用系统的行分隔符而不是硬编码的行分隔符.

This way you use the System's line separator rather than a hard coded one.

这样您的代码将如下所示:

So that your code would look like this:

    public void addLine() {
       String lineseparator=System.getProperty("line.separator");
   // I'd suggest putting this as a class variable, so that it only gets called once rather
   // than
   // everytime you call the addLine() method
    try {
        FileWriter stream = new FileWriter(this.log, true);
        //If you don't add true as the second parameter, then your file gets rewritten
       // instead of appended to
        BufferedWriter out = new BufferedWriter(stream);

        out.write(lineseparator); //This substitutes your out.newline(); call

       out.close();
       stream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

################################################ ###########################.

我会尽量简洁明了.

##############################################################################.

I will try to be as brief and clear as possible.

我假设您正在打开一个文件,该文件在我的代码中称为"test.txt",大约有一个段落.并希望将其输出到另一个文件,但有时会出现空行".

I assume that you are opening a file that in my code I call "test.txt" and it's got about a paragraph or so. And that you want that outputted to another file, but with "empty lines" at some points.

由于File()是逐行读取的,因此打开主文件先读取一行,然后将其写入日志文件,然后分析是否需要空行要容易得多并放置它.

Because File() is read line by line, it is much easier to open your main file read a line, and then write it to your log file, then analyse if an empty line is necessary and place it.

那我们来看一些代码.

// Assume you have a private class variable called
private String lineseparator=System.getProperty("line.separator");

// This method is in charge of calling the method that actually carries out the 
// reading and writing. I separate them both because I find it is much cleaner
// to have the try{}catch{} blocks in different methods. Though sometimes for
// logging purposes this is not the best choice
public void addLines() {
    try {
        readAndWrite();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

// This method is in charge of reading one file and output to another. 
public void readAndWrite() throws IOException {
    File test = new File("test.txt");
 FileWriter writer = writer = new FileWriter(new File("log.txt"), true);
     //This FileWriter is in charge of writing to your log file

 String line;
 boolean conditionToWriteNewLine=true;
      //Obviously this needs to be changed to suit your situation
      // I just added it for show

 BufferedReader reader = new BufferedReader( new FileReader (test));
 BufferedWriter out = new BufferedWriter(writer);

     //It is in this while loop that you read a line
     // Analyze whether it needs to have a new line or not
    // and then write it out to log file
 while( ( line = reader.readLine() ) != null ) {
        out.write(line); 
        if(conditionToWriteNewLine){
           out.write(this.lineseparator); 
           out.write(this.lineseparator);
               //You need to write it twice for an EMPTY LINE
        }     
 }
 reader.close();
 out.close();   
 }

与该代码的最大区别之一是,我仅一次打开文件,而在您的代码中,每次要添加新文件时都打开日志文件.您应该阅读文档,这样您才能知道,每次打开文件时,光标都指向第一行,因此您添加的所有内容都会添加到第一行.

One of the big differences from this code is that I only open the files once, while in your code you open the log file every time you want to add a new file. You should read the documentation, so you'll know that every time you open the file, your cursor is pointing to the first line, so anything you add will be added to first line.

我希望这可以帮助您了解更多信息.

I hope this helped you understand some more.

这篇关于JAVA我在做什么错,我要线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆