2016-03-05 171 views
0

我有一条nmea消息。我想写出一个只有$GPGSV行的其他文件。这里是我的代码,它现在为每行写入布尔值(true,false)。我怎样才能写出行的值?将指定的行写入新文件

public static void main(String[] args) throws IOException { 
     File inputFile = new File("proba.txt"); 
     File tempFile = new File("kiir.txt"); 

     BufferedReader reader = new BufferedReader(new FileReader(inputFile)); 
     BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); 

    String currentLine; 

    while((currentLine = reader.readLine()) != null) { 
      // trim newline when comparing with lineToRemove 
      String trimmedLine = currentLine.trim(); 

    writer.write(currentLine.startsWith("$GPGSV") + System.getProperty("line.separator")); 
    } 
    writer.close(); 
    reader.close(); 
+1

在写入时使用'if'(或者请求原作者为您添加一个) – 2016-03-05 08:05:09

+0

一些阅读:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html – 2016-03-05 08:06:45

+0

一些示例输入和输出行,以便每个人都可以了解你在代码中缺少的内容? – Mahendra

回答

1

startsWith()返回一个布尔值,让你用它的方式,它只会打印truefalse到文件。试试这个

if(trimmedLine.startsWith("$GPGSV"){ 
    writer.write(trimmedLine + System.getProperty("line.separator")); 
} 

编辑:代替currentLine使用trimmedLine。我认为这是你想要使用的。