English 中文(简体)
从文字档案中读取,按性质
原标题:Reading in from text file character by character
  • 时间:2011-10-29 20:42:03
  •  标签:
  • java
  • text
  • io

在 Java,用一种方式阅读档案(文本档案),只读一种特性,而不是强硬。 这样做是为了极端基本的灵活性,因此,你可以理解为什么我想要这样做。 谢谢。

问题回答

此处为阅读/写作某一特性的样本代码

public class CopyCharacters {
    public static void main(String[] args) throws IOException {

        FileReader inputStream = null;
        FileWriter outputStream = null;

        try {
            inputStream = new FileReader("xanadu.txt");
            outputStream = new FileWriter("characteroutput.txt");

            int c;
            while ((c = inputStream.read()) != -1) {
                outputStream.write(c);
            }
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
        }
    }
}

注,这一答复经过更新,以复制Ref链接中的样本代码,但我认为这基本上与下文所述的答复相同。

ref: http://download.oracle.com/javase/tutorial/essential/io/charstreams.html

您可以使用从投放器类别中读取的方法,即从下游到下游的1级和回归1级。

   public static void processFile(File file) throws IOException {
        try (InputStream in = new FileInputStream(file);
             Reader reader = new InputStreamReader(in)) {

             int c;
             while ((c = reader.read()) != -1) {
                processChar((char) c);  // this method will do whatever you want
             }
        }
    }

你们可以读一下整个档案(如果它不是大的),记号为示意图,并按特性重写。

有若干可能的解决办法。 一般来说,您可使用<代码>Reader>,java.io的读物包,例如:

// Read from file
BufferedReader reader = new BufferedReader(new FileReader("file.txt"));
// Read from sting
BufferedReader reader = new BufferedReader(new StringReader("Some text"));




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签