You can do something like this:
BufferedReader reader = new BufferedReader(new FileReader(<<your file>>));
List<String> lines = new ArrayList<>();
String line = null;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
System.out.println(lines.get(0));
With BufferedReader you are able to read lines directly. This example reads the file line by line and stores the lines in an array list. You can access the lines after that by using lines.get(lineNumber).
You can do something like this:
BufferedReader reader = new BufferedReader(new FileReader(<<your file>>));
List<String> lines = new ArrayList<>();
String line = null;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
System.out.println(lines.get(0));
With BufferedReader you are able to read lines directly. This example reads the file line by line and stores the lines in an array list. You can access the lines after that by using lines.get(lineNumber).
You can read text from a file one line at a time and then do whatever you want to with that line, print it, compare it, etc...
// Construct a BufferedReader object from the input file
BufferedReader r = new BufferedReader(new FileReader("employeeData.txt"));
int i = 1;
try {
// "Prime" the while loop
String line = r.readLine();
while (line != null) {
// Print a single line of input file to console
System.out.print("Line "+i+": "+line);
// Prepare for next loop iteration
line = r.readLine();
i++;
}
} finally {
// Free up file descriptor resources
r.close();
}
// Remember the next available employee number in a one-up scheme
int nextEmployeeId = i;
Reading CSV file.
Reading one specific line from CSV file
java - How do I efficiently read random lines from a TXT or CSV file? - Software Engineering Stack Exchange
How to read and print only specific rows of a CSV file?
Videos
Ok im trying to read from a csv file and some how my directory can't find it can someone help. The Cvs file is in my project src file which I have named Files as well. Heres the code: import java.io.BufferedReader;
import java.io.FileReader; import java.io.IOException; import java.io.*; public class Operrations {
public static void main(String[] args) throws Exception {
String File = "Files\\Crimes.csv";
BufferedReader reader = null;
String line = "";
try {
reader = new BufferedReader(new FileReader(File));
while((line = reader.readLine()) !=null);
String[] row = line.split(",");
for(String index: row){
System.out.printf("%-10", index);
}
System.out.println();
}
catch (Exception e){
e.printStackTrace();
}
finally {
try{
reader.close();
} catch(IOException e){
e.printStackTrace();
}
}
}}
Hi all! I'm wondering how I could solve the following problem. Let's say I have a .csv file that looks like this:
| ID | Name |
|---|---|
| 20943 | Alice |
| 19782 | Bob |
I also have a class, let's call it Person :
public class Person {
private int ID
private String name
public Person(int ID, String Name) {
this.ID = ID;
this.name = name;
}
}
Now I'd like to create some sort of PersonDataReader class:
public class PersonDataReader {
public Person createPersonFromData(int line) {
}
}I'd like this createPersonFromData method to jump to the specified line, read the name and ID and then call the constructor of Person with those arguments. How can I achieve something like that?
CSV files and many other text based formats encode information in a potentially variable size. Due to the variable record size, there is no way to directly jump to a random record if you don’t know its precise position in the file.
To get some randomness in your app, you could imagine to go to a random offset in the file and move forward or backward to the next record by looking for a line separator. However, you will not know which record it is. Moreover, if you use the CSV variant defined in RFC4180, this will not work, because line separators can be part of a field if it is enclosed between quotes, and you wouldn’t know if there wasn’t such an unclosed opening quote before. The only way is then to read the file from the start and parse it record after record.
What you need to do is to construct an index. , as suggested by Killian and Amon in the comments. For example:
- at start of your programme by parsing your data file, and build in memory an array with the starting position of the record in the file.
- or, use a separate indexer that parses the file, and created an index file. Each index record is of fixed size and indicates the position of the next record in the main data file. Since the size of index record is fixed, you can locate easily any random record
nby going to positionn*sizeOfRecordin the index file, read the position of where to find the record in the data file, and start reading the data at this offset.
Remark: this indexing approach works with any variable length encoding. It could be multiline files, or even complex formats such as json or xml, provided that the indexer can parse the expected format to identify the boundaries of each record.
While Christophe's answer addresses the actual question about the random reading of the text files, I think there is a XY problem here.
You have a set of chess puzzles structures in a form of a text file. And so you're asking how to read a random line from this file. Your actual question should instead be how to load a random chess puzzle, given that currently, the puzzles are stored in a given format.
The answer then involves two stages.
The first stage is to find what's the correct format to store the puzzles in the first place. For your needs, the text format is not a correct one. It doesn't allow it easily to load a random puzzle, and it possibly wastes too much disk space anyway. Instead, one may use a relational database. If the data needs to be distributed with your application (i.e. to work in offline mode), SQLite could be a great option. It will handle all the storage details for you, so you would be able to retrieve a puzzle in just one line of code.
NoSQL databases may also be considered, in order to store the puzzle in a very readable (such as JSON), but also very efficient (i.e. compressed) way. This could further simply the code, in your application, which would convert the data received from the database to the actual object in the application itself.
The second stage then is to export the data from the text file, and actually import it to the database.