How can i write to a file my arraylist? [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 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.

Related

Large string input isn't being read completely, on an online judge [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 4 days ago.
Improve this question
There's a question on Codechef. A test case in this, where a string of length 100000, is being input. But when i print the length of string after input, its only coming out to be 65526. Why is that so? Is the input not being taken properly?
For input I use,
JAVA CODE:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 131072);
int n = Integer.parseInt(br.readLine());
String c = br.readLine();
NOTE : I also tried doing it in C++, but same problem occurs there.
I also tried, increasing the buffer size in case it causes problem, but no effect.
Also, Scanner didn't work in java!
Can someone please tell me what's going wrong?

Read a csv file into one String [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 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.

Java the best and fastest way to edit text file [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I need to edit a lot of text files effectively and fast! What is the best thing I can do?
I already come up with this function:
private boolean edit(File source)
{
if (!source.getAbsolutePath().endsWith(".java")) //Java text files only
return false;
String l, str = "", orig = "";
try
{
BufferedReader r = new BufferedReader(new FileReader(source));
while ((l = r.readLine()) != null)
{
orig = str += l+"\n";
}
r.close();
for (Entry<String, String> e : mappings.entrySet()) //Replacing string by HashMap mappings!
str = fastReplace(str, e.getKey(), e.getValue()); //Faster alterntive to String#replaceAll
if (!str.equals(orig))
{
BufferedWriter bf = new BufferedWriter(new FileWriter(source));
bf.write(str);
bf.close();
return true;
}
}
catch (Exception e)
{
doLog(e.toString()); //Logging exception but unimportant for us...
}
return false;
}
I found my function a little bit clumsy because it first needs to read text file into string then edit it and write it back after that. So the question is. Is there any better and faster way to edit text file? I mean for example without necessity to turning it into string and then writing it back. For example is there a way to edit file directly as a text file or writing it without overriding the same unchanged parts of the file or any faster way to read and write file? Or is my function actually already fastest as it can be?
In case somebody is wondering what my "fastReplace" function does then check this Faster alternatives to replace method in a Java String? but I do not think it is important.
If you need to replace a string by another one of the exact same size byte for byte, then you could read the data sequentially in chunks with multiple block size, replace the desired spots and write the data back if a change has been made. If no change was made then there is no need to write back the block. In the best scenario, you will save few I/O operations, at the cost of significant code complexity.
If your editing is more complex and involves string insertions, then you have no way out of reading and writing back the entire text.
Early optimization is a bad idea. Source code files hardly span a single block and in your case you will probably save no time or space.

How to write a LocalDate list to a file 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 4 years ago.
Improve this question
I'm trying to write a LocalDate list to an output file, i've got List<LocalDate> fechas but when i try this code:
FileWriter writer = new FileWriter("output.txt");
for(LocalDate str: fechas) {
writer.write(str);
}
writer.close();
it doesn't compile
write takes a String - you'll have to convert your LocalDate to a stirng:
for (LocalDate fecha: fechas) {
writer.write(fecha.toString());
}

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";
}

Categories