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.
Related
I want to copy all of content from one file to another existing file, to a specific line.
File 1, File 2, Expected result
I used FileWriter to write both files, now I don't know how to merge them like this. I know how to write from one to another file but only to append one files content to the end of another file. But I need it to copy one file's content to another but at specific line, like in example pics. In example if you look, content is copied between two brackets of class ({}). Any ideas?
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
I've been googling and I've not found one example for this.
I am able to extract the contents for a DOCX file but so far no clue how to get the contents of an EXCEL file.
I know you use
SpreadsheetMLPackage spreadsheetMLPackage = SpreadsheetMLPackage.load(file);
to load the file, but I don't know how to proceed from here. I've check whatever methods SpreadsheetMLPackage has but nothing has gotten me the contents.
First you need to understand the structure of a xlsx file.
Unzip one, or run it through the docx4j webapp.
For how the parts relate to one another, see:
http://openxmldeveloper.org/blog/b/openxmldeveloper/archive/2007/08/13/1970.aspx
I guess the key method you'll want is getWorksheet
But first you'll need to get the WorkbookPart; do that with spreadsheetMLPackage.getWorkbookPart()
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 am new to pentaho kettle...
For now, I have a folder contain many .txt files.
Let say for example: 20121012.txt, 20121014.txt.....
Everytime I run the kettle job, it will grep all these files for import into database.
I need to handle the checking before import into db to prevent data duplication.
The problem is that, how can I let the kettle notice the filename which is already imported?
For example:
20121012.txt <=if this file is imported, it will check the filename of it on next time, if it is same filename, then it will be not imported.
In this case, I cannot just simply set a specific file "20121012.txt" in the step "Check if files exists". It was because the txt file is large amount. If the filename refer to a day, then 1 year contain 365-366 days. I cannot hard code all days file in this way.
So, the possible way is to check the filename of that process file whether the filename is existed before import into database.
And that is my question that how can I do this? What steps or work flow I need to use?
Could anyone provide the detail step that is possible to do this?
I am looking forward to hearing from you and please let me know if you need more information.
Thanks all for helping!
You can do this by storing the already processed file list in a place like a table in the database. Load in the table in another step, then join the streams from the steps with a merge and pass through only those files from the file load step that are not in the other stream.
Make sure to later update your already processed table with any newly processed files later on.
You can use "Get File Names" step. In this step: set the folder(s) which store your files, and then set the wildcard (for example ".*" if you want all files from folder).
If your database stores already imported filenames, you can make your transformation indepotent by using "Database Lookup" to check if your filename is already in database, and then filter on a stream, to pass only filenames that weren't found in the database.