How to write to one file from another at specific line? - java

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?

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?

How to read/print the contents for excel file using docx4j?

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()

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

Saving to a folder

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

How to append existing line within a java text file

I'm having trouble adding to an exsisting line in a text file without overwriting that particular line or adding a new line.
for example, i have a line in my text file which is:
hello my name is
I would like to add to this line so it becomes:
hello my name is joe bloggs
Thanks
i have a task to create a help desk program and i am trying to incorporate a feature that enables users to edit questions they have posted. as a result, the program will need to be able to append Any line within the text file - not necessarily just the last line
If it's not at the end of the file, you're in trouble - you're basically talking about inserting data in the middle of a file, which isn't traditionally supported by file systems.
The normal way to approach this is to create a new file - copy the portion before the insertion point from the old file, then write your new data, then copy the remainder of the original file afterwards. Finally, do whatever renaming/deleting you need.

Categories