【问题标题】:File Handling in Java ProgrammingJava 编程中的文件处理
【发布时间】:2013-06-12 14:09:52
【问题描述】:

我是编程初学者。

我正在尝试从文件中读取每一行的第一个单词,并且我想将所有第一个单词写入另一个文件。

我已经完成了以下代码。

import java.io.*;
import java.nio.charset.Charset;

public class ReadWord
 {
   public static void main(String args[])
     {
    BufferedReader br;
    String line;
    int count = 0;
    InputStream fis = null;

    try
    {
        fis = new FileInputStream("D:/Android/WorkSpace10-1/FileHandling.Java/src/123.txt");
        br = new BufferedReader( new InputStreamReader(fis, Charset.forName("UTF-8")));
        while((line = br.readLine())!=  null)
        {
            System.out.println(line);
            String result[] = line.split(",");
            while(line!=",")
            {                   
              System.out.println(result);               
            }               
        }               
    }

    catch(Exception e)
    {
        System.out.println(e);
    }

    finally
    {
        try
        {
            fis.close();
        }
        catch(IOException ie)
        {
            System.out.println(ie);
        }
    }
}
}

请帮助我如何读取每行的第一个单词并将该单词写入另一个文件。

【问题讨论】:

  • 当前输出是多少?
  • 如果您使用 Java 7,请使用 Files!
  • @colti.. 垃圾输出。它进入无限循环..
  • @fge.. 这就是我正在使用的..
  • 错了,你确定吗?看我的回答...

标签: java file-handling


【解决方案1】:

我不确定这是您可以像以下示例那样拆分每一行的最佳方法:

String hello = "Hello world, How is everyone";
String[] split= hello.split(" ");
String first = split[0];

........

然后像这样使用 BufferedWriter 将其写入文件:

BufferedWriter bw = new BufferedWriter(new FileWriter("MyFIle.txt"));

bw.write(first);
bw.newLine();

bw.close();

这只是一个指南。

【讨论】:

    【解决方案2】:

    帮自己一个忙:使用Files。

    获取文件的BufferedReader 非常简单:

    final BufferedReader reader 
        = Files.newBufferedReader(Paths.get("pathToFile"), StandardCharsets.UTF_8);
    

    获取BufferedWriter 非常简单:

    final BufferedWriter writer
        = Files.newBufferedWriter(Paths.get("pathToFile"), StandardCharsets.UTF_8);
    

    从文件中读取所有行很简单:

    final List<String> lines 
        = Files.readAllLines(Paths.get("pathToFile"), StandardCharsets.UTF_8);
    

    等等。见the javadoc for Files。任何使用 Java 7 的人都没有理由再使用 File!

    【讨论】:

      【解决方案3】:

      我为你编写了一个程序来读取文件中每一行的第一个单词,然后写入另一个文件。这不是一个完美的程序,但它可以工作,至少应该提供一个起点。

      String separator = System.getProperty( "line.separator" );
                       BufferedReader br;
                       String line;
                       InputStream fis = null;
                       File file = new File("C:\\junk\\out.txt");
                          // if file doesnt exists, then create it
                          if (!file.exists()) {
                              file.createNewFile();
                          }
                       FileWriter fw = new FileWriter(file.getAbsoluteFile());
                       BufferedWriter bw = new BufferedWriter(fw);
      
                       try
                       {
                           //your input file
                           fis = new FileInputStream("C:\\junk\\test.txt");
                           br = new BufferedReader( new InputStreamReader(fis, Charset.forName("UTF-8")));
                           while((line = br.readLine())!=  null)
                           {
                               System.out.println(line);
                               String result[] = line.split(" ");
                               String word = result[0];
                               bw.write(word);
                               bw.write(separator);
                           }    
                       }
      
                       catch(Exception e)
                       {
                           System.out.println(e);
                       }
      
                       finally
                       {
                           try
                           {
                               //close the files
                               fis.close();
                              bw.close();
                           }
                           catch(IOException ie)
                           {
                               System.out.println(ie);
                           }
                       }
      

      【讨论】:

      • @stupefy 欢迎,请随时为此答案提供 1+。很高兴我能帮上忙。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 2011-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多