Reading a Plain Text File in Java

Reading a plain text file in Java

ASCII is a TEXT file so you would use Readers for reading. Java also supports reading from a binary file using InputStreams. If the files being read are huge then you would want to use a BufferedReader on top of a FileReader to improve read performance.

Go through this article on how to use a Reader

I'd also recommend you download and read this wonderful (yet free) book called Thinking In Java

In Java 7:

new String(Files.readAllBytes(...))

(docs)
or

Files.readAllLines(...)

(docs)

In Java 8:

Files.lines(..).forEach(...)

(docs)

Reading a plain text file

You can reach a file from context in android.

Context Context;
AssetManager mngr = context.getAssets();
String line;
try {

BufferedReader br = new BufferedReader(new FileReader(mngr.open("words.txt")));
if (!br.ready()) {
throw new IOException();
}
while ((line = br.readLine()) != null) {
words.add(line);
}
br.close();
} catch (IOException e) {
System.out.println(e);
}

Or try this:

String line;
try {

BufferedReader br = new BufferedReader(new FileReader(getApplicationContext().getAssets().open("words.txt")));
if (!br.ready()) {
throw new IOException();
}
while ((line = br.readLine()) != null) {
words.add(line);
}
br.close();
} catch (IOException e) {
System.out.println(e);
}

Reading and displaying data from a .txt file

BufferedReader in = new BufferedReader(new FileReader("<Filename>"));

Then, you can use in.readLine(); to read a single line at a time. To read until the end, write a while loop as such:

String line;
while((line = in.readLine()) != null)
{
System.out.println(line);
}
in.close();

how to Read data from a text file in java to extract data using StanfordNLP rather than reading text from a simple String

Use as below (see the example given here):

// creates a StanfordCoreNLP object, with POS tagging, lemmatization, NER, parsing, and coreference resolution 
Properties props = new Properties();
props.put("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);

// read some text from the file..
File inputFile = new File("src/test/resources/sample-content.txt");
String text = Files.asCharSource(inputFile, Charset.forName("UTF-8")).read();

// create an empty Annotation just with the given text
Annotation document = new Annotation(text);

// run all Annotators on this text
pipeline.annotate(document);

// these are all the sentences in this document
// a CoreMap is essentially a Map that uses class objects as keys and has values with custom types
List<CoreMap> sentences = document.get(SentencesAnnotation.class);

for(CoreMap sentence: sentences) {
// traversing the words in the current sentence
// a CoreLabel is a CoreMap with additional token-specific methods
for (CoreLabel token: sentence.get(TokensAnnotation.class)) {
// this is the text of the token
String word = token.get(TextAnnotation.class);
// this is the POS tag of the token
String pos = token.get(PartOfSpeechAnnotation.class);
// this is the NER label of the token
String ne = token.get(NamedEntityTagAnnotation.class);

System.out.println("word: " + word + " pos: " + pos + " ne:" + ne);
}

Update

Alternatively, for reading the file contents, you could use the below that uses the built-in packages of Java; thus, no need for external packages. Depending on the characters in your text file, you can choose the appropriate Charset. As described here, "ISO-8859-1 is an all-inclusive charset, in the sense that it is guaranteed not to throw MalformedInputException". The below uses that Charset.

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

...
Path path = Paths.get("sample-content.txt");
String text = "";
try {
text = Files.readString(path, StandardCharsets.ISO_8859_1); //StandardCharsets.UTF_8
} catch (IOException e) {
e.printStackTrace();
}

Issue with reading data from a txt file

I do not see four spaces in your data files, but the error message is 'For input string: "0 0"'
I guess you have a tabulation in your file. You do not see it in your editor, but it might was converted to four spaces.
Try to replace your

split(" ");

to

split(""\\s+""); \\ Thanks for the comment, I did not think about several whitespace characters.

"\s+" will understand several spaces and the tabulations as well.
I hope this helps.

How to find, read and display a line from a text file with Java

In case 2 ask the user again for an ID and read it with Scanner.

Create a method where you iterate in a loop through your airplaneSeats array, and if the entered ID matches the ID of one of the AirplaneSeatRecords in your array, then print the record and break the loop. You already have the necessary methods for it in the AirplaneSeatRecords class.

Also, don't forget to add a default case to your switch statement.



Related Topics



Leave a reply



Submit