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
Related
I am trying to read all files in a folder and merge them into different file. When the file is merged I want to make sure the merged file content is in sorting order.
Content of each file will be in this format.
TimeStamp, Number1, Tesxt1
2020-01-22 11:22:33.323,12313,test1
Currently i have created java program to sort and merge two files. but I want to make sure i can read from all files in dictory and merge to one (sorting by timestamp).
Can someone kindly help me with the code please. I am open for any technology. Java or Python and etc.,
In Linux, this can be done as a one-liner using standard utilities:
cat file1 file2 file3 | sort > file4
Seeing that your timestamp is the first field and in YMD HMS format, a simple alphabetic sort will render the file in chronological order. Here, three files are concatenated, then passed through sort (default is alphanumeric, ascending order), and the result redirected to a new file.
Windows batch utilities should have the same capabilities.
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.
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.
I'm writing an application that is writing to a file. I'm wondering if it's possible to write to a folder, without specifying a file name. The way I have it set up now, my program will overwrite the previous saved file. I'm looking to have it add to the folder rather than replace.
Here's the line in question:
File testFile = new File("C:/TargetFolder/testFile.png");
There is no way to write to a file without assigning a file name. However, if you don't want to chose a file name you can have your system generate a random one. For example look at these options: What is the best way to generate a unique and short file name in Java.
Another option would be to add numbers to your file name like: test01.png, test02.png and so on.
If you don't want to do the unique file in the folder logic yourself and you don't care much about the exact name, you might use:
java.io.File.createTempFile("testFile", ".png", new File("C:/TargetFolder"));
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.