繁体   English   中英

读取Java中的txt文件

[英]Reading txt files in Java

我有一个程序,其中有一个部分需要我阅读并将 append 项目写入 txt 文件。 我知道如何进行基本的读取和附加,但我对如何读取 txt 文件中的每 4 行然后将其存储在变量中感到困惑。 甚至每条备用线。 另外,如果有双值数字,我可以将其读取为数字而不是字符串吗?

要从文本文件中每隔四行读取一次,您将读取一行并更新一个计数器。 当计数器达到4时,您将该行保存在 String 变量中。 像这样的东西可以完成这项工作:

import java.io.*;
public class SkipLineReader {

    public static void main(String[] args) throws IOException {
        String line = "";
        String savedLine = "";
        int counter = 0;
        FileInputStream fin = new FileInputStream("text_file.txt");
        BufferedReader bufIn = new BufferedReader(new InputStreamReader(fin));
        
        // Save every fourth line
        while( (line = bufIn.readLine()) != null) {
            counter++;
            if( counter == 4 ) {
                savedLine = line;
                System.out.println(line);
            }
        }
    }
    
}

要保存每一行,您将在每次计数器达到 2 时保存该行,然后将计数器重置为零。 像这样:

        // Save every alternate line
        while( (line = bufIn.readLine()) != null) {
            counter++;
            if( counter % 2 == 0 ) {
                counter = 0;
                savedLine = line;
                System.out.println(line);
            }
        }

至于从文件中读取双精度值,您可以使用BufferedReader来完成,然后使用 Double 的parseDouble(string)方法来检索双精度值,但更好的方法是使用 java.util 中的Scanner java.util 此 class 的构造函数将接受FileInputStream并且有一个nextDouble()方法可用于读取双精度值。

下面是一些代码,说明了使用Scanner object 从字符串中获取双精度值(要从文件中读取,将FileInputStream提供给Scanner类的构造函数):

import java.util.*;

public class ScannerDemo {
   public static void main(String[] args) {

      String s = "Hello World! 3 + 3.0 = 6 true";

      // create a new scanner with the specified String Object
      Scanner scanner = new Scanner(s);

      // use US locale to be able to identify doubles in the string
      scanner.useLocale(Locale.US);

      // find the next double token and print it
      // loop for the whole scanner
      while (scanner.hasNext()) {

         // if the next is a double, print found and the double
         if (scanner.hasNextDouble()) {
            System.out.println("Found :" + scanner.nextDouble());
         }

         // if a double is not found, print "Not Found" and the token
         System.out.println("Not Found :" + scanner.next());
      }

      // close the scanner
      scanner.close();
   }
}

这是我的代码示例。

public static void main(String[] args) throws Exception {
    // Read file by BufferedReader line by line.
    BufferedReader reader;
    try {
        reader = new BufferedReader(new FileReader("test.txt"));
        String line = reader.readLine();
        while (line != null) {
            line = line.trim();
            System.out.println(line);
            
            // Using regular expression to check line is valid number
            if (!line.trim().equals("") && line.trim().matches("^\\d+||^\\d+(\\.)\\d+$")) {
                double value = Double.valueOf(line.trim());
                System.out.println(value);
            } else {
                String value = line.trim();
                System.out.println(value);
            }
            
            // Read next line
            line = reader.readLine();
        }
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM