Read multiple .txt files and write information to an Array - java

I have a method that creates files containing the name and age and writes, Example "name.txt" and the content is .. name-age .. would like to get this information from 10 .txt files and write in a single file, line after line

You can iterate through files inside a directory. In each file you can iterate through lines and print them out.

Related

Java - Replace specific line in file without copying all data to another file

I have some text file named data.dat. The second line contains a number which I would like to update to another number. The file can get pretty huge so I don't want to create any temporary files and copy over all this data to another file just to change a single line. Is there a way?

reading two text files directories in java and combining their content

im new to java and i want to write a program that gets two directories as an input, the directories contain .txt files. the program should read files from both directories and combine the content of those with the same name into a new file and put in a new directory.
the files contain DNA sequences
i would appreciate any help with hints or ways on how to achieve this.
You can open two files simultaneously in java using two different objects.
Now store all the desired contents from file 1 into string variable.
Do the same with second file(create the two diff string).
And then merge the data under new string variable.Now contents of the newest string can be transfered into new file

How can I output an array with a bunch of assignments from a folder of files?

I have 151 images I would like stored in an array that includes their file path and some attributes which will be String data extracted from the file names.
I am guessing I'll be using File IO/NIO for this but of these two options:
write the array from the disk every time the program is run
write the array once with a throwaway program so I can just copy the
code of the array and have it be hardcoded
Two seems much more sensible. I just don't know how
Check if there any previous record. If there are no records, write the array and save it. If there are records, read from it.

Read write file in specific manner

I have read and write file in a specific manner.
Format of file:
a1(1,2,5,8,0);
a2(4,6,8,4);
a7(4,5,7,8);
At a time if I modify section for a2 say like "2,4,6,8", how should I modify the file.
And how can I read values for specific title. That is, if I want to fetch values only for a7, then the method should return 4,5,7,8.
Thank You
"how should I modify the file"
To modify the content of the file, you need to write it into the temporary file. After you finish, delete the original and rename the temporary file as the original file name.
"how can I read values for specific title"
Read the file line by line and search for your specific title:
if(line.contains("a7")){
System.out.println(line.substring(line.indexOf("("),line.indexOf(")")+1));
}

Reading files from a directory and sending files to another class

I have two classes that I'm working with, the first which just finds the file and sends it to the second class, which does the work I need it to do.
So far, I've only managed to get the first class to read through a directory and print the names of the files. This is the code I use to see the names:
File folder = new File("data\\");
File[] listFiles = folder.listFiles();
for (File file : listFiles){
if (file.isFile()){
System.out.println(file);
}
}
However, i need to send the files over to the other class to read through. Up until now, I was doing
Grader.getFileInfo("data\\studentSubmissionA.txt");
Grader.teacherFiles("data\\TeacherListA.txt");
to send the file information over, but this isn't as convienient as using the array to go through the entire directory.
However, I don't know how to get it to send the files over to the other class one by one in order for the class to do its work.
I've been browsing the internet for hours and only found things which list the file names and that's not what I need to do. I actually need to send the files and their content over to the next class.
Is there any way to actually do this? And how would i go about doing it?
I just want to use an arraylist if possible, so no try/catch, or BufferedReader if possible since I've been using scanner.
What I'm trying to say is that instead of printing the names of the files in the directory, as the array does now, I would like it to iterate through and send the information in the files to the second class as the Grader.getFileInfo() calls do.

Categories