Read a csv file into one String [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 9 months ago.
Improve this question
I want to essentially read the contents of csv file and want to input into one string to return. I tried this and it doesnt quite work:
File file = new File(aaa.csv);
FileWriter fw = new FileWriter(file);
BufferReader bw = new BufferReader(fw);
String s;
while (bw.readLine() != null) {
s += (bw.readLine());
}
fw.close();
Is there a easier solution to reading csv file into string that works?

To read the entire file, assuming Java 11 and above, you can simply do:
String content = Files.readString(Paths.get("aaa.csv"));
note that if the file is very large memory becomes a problem.

Related

how to split a text file by line gaps in java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am reading a text file in Java that looks like this,
"
Q1. You are given a train data set having 1000 columns and 1 million rows. The data set is based on a classification problem. Your manager has asked you to reduce the dimension of this data so that model computation time can be reduced. Your machine has memory constraints. What would you do? (You are free to make practical assumptions.)
Q2. Is rotation necessary in PCA? If yes, Why? What will happen if you don’t rotate the components?
Q3. You are given a data set. The data set has missing values which spread along 1 standard deviation from the median. What percentage of data would remain unaffected? Why? "
Now, I want to read this file and then store each of these sentences(questions) in a string array. How can I do that in java?
I tried this,
String mlq = new String(Files.readAllBytes(Paths.get("MLques.txt")));
String[] mlq1=mlq.split("\n\n");
But this is not working.
Try this
String mlq = new String(Files.readAllBytes(Paths.get("MLQ.txt")));
String[] mlq1=mlq.split("\r\n\r\n");
System.out.println(mlq1.length);
System.out.println(Arrays.toString(mlq1));
This should do it by line gap of 2 lines.
File file = new File("C:\\MLques.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
while ((st = br.readLine()) != null) {
System.out.println(st + "\n");
}
I think it will work.
This is a piece of code from one of my project.
public static List<String> readStreamByLines(InputStream in) throws IOException {
return IOUtils.readLines(in, StandardCharsets.UTF_8).stream()
.map(String::trim)
.collect(Collectors.toList());
}
But!!! If you have really big file, then collecting all content into a List is not good. You have to read InputStream line by line and do all you need for every single row.

BufferedReader gives null when file is not in Documents [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am working on a text editor software(kinda like notepad) and whenever I open files from Documents, correct data is displayed but at any other location Null is returned
This is because bff.readLine() is returning null. According to the documentation, it returns null if the end of the stream has been reached.
The previous check bff.readLine() != null doesn't help, because each call advances the reader. Try it like that:
String line;
while ((line = bff.readLine()) != null) {
sk += line + "\n";
}

How can i write to a file my arraylist? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I make some changes in my arraylist. After I want to write to a txt file from my arraylist. But it write just onu line. I didn't get it.
How can i fix it?
This is my code
try {
show_seats_write = new PrintWriter(new FileOutputStream(new File("d:/seats2.txt"),true));
System.out.println("Tickets size: "+tickets.size());
for(int i=0;i<tickets.size();i++){
for(int j=0;j<tickets.get(i).getlist().size();j++){
show_seats_write.print(tickets.get(i).getlist().get(j));
System.out.print(tickets.get(i).getlist().get(j));
System.out.println();
show_seats_write.println();
show_seats_write.close();
}
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
You're closing the PrintWriter after only writing one thing. Move show_seats_write.close(); outside of your for loops.
show_seats_write.print(tickets.get(i).getlist().get(j));
This line should either be;
show_seats_write.println(tickets.get(i).getlist().get(j));
// OR
show_seats_write.print(tickets.get(i).getlist().get(j) + "\n");
I think that should get your info onto multiple lines.

How to read previous line by using bufferedReader [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am making a some large project. In that I am stuck at one place. I am using the following code to read from the file:
String str = null;
FileReader fr = new FileReader("H:\\Eclipse\\Emulator\\progin8085.txt");
BufferedReader br = new BufferedReader(fr);
str = br.readLine(); //using it in a loop from 0 to total_no_of_lines
Now I when I reached suppose at line 8(counting lines numbers while entering data to the file), I want to go back to line 3 or 4 or any and again want to read and execute each statement. How to read previous statements using BufferedReader only? If it is not possible, any other solution?
Some Readers support marks. You can use those to rewind the file. A particularly useful Reader (imo) is the LineNumberReader and it supports marks. This sort of code might suit your needs.
public static int final READ_AHEAD_LIMIT = 100000;
LineNumberReader lnReader = new LineNumberReader(reader);
while (youWantToRead) {
...
if (mightBeInterestingLater) {
lnReader.mark(READ_AHEAD_LIMIT);
}
...
if (nowWantToRewind) {
lnReader.reset();
// We're now at whatever place mark() was last called at.
}
Save all the lines of text file in String[] StrArray and do whatever you want.

Moving from buffer to an array for manipulation in Java [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am making a program to take data from a .txt document, I have used the BufferedReader method which goes through the document. It then prints the the documents contents into the console. I cannot however for the life of me figure out how to get the contents from the BufferedReader into a string array so I can then further manipulate it. Any help?
The method Files.readAllLines reads a file into a List<String>, one String per line in the original file. The method Files.readAllBytes just reads a file into a byte array if that's your style. These are both part of the Java NIO 2 library, part of Java SE 7.
Path myFile = Paths.get("hello_world.txt");
List<String> fileLines = Files.readAllLines(myFile);
use DataInputStream instead of BufferedReader
DataInputStream is = new DataInputStream (new FileInputStream ("myFileName"));
then you can store a String with methtod call is.readUTF ();
If you want a String[] array containing the lines of the document, you could use BufferedReader.readLine(), and stuff the lines into a List<String> instead of printing them.
Here's how you can create a list of Strings:
List<String> lines = new ArrayList<String>();
And here's how you can add to it:
String line = null;
while( (line = reader.readLine()) != null ) {
lines.add( line );
}

Categories