This question already has answers here:
How to append text to an existing file in Java?
(31 answers)
Closed 7 years ago.
I want to write particular string in a file after a string. My file has this already -
##############################
path :
I need to write the String /sdcard/Docs/MyData after path :
Could anyone tell me how I could achieve this?
If I understand correctly you mean to append your path at the end of your file.
If so the use of a FileWriter is a good way to do it.
new FileWriter("Your path", true)
Notice that the boolean true in this case indicates that you want to append to your file, removing this altogether or using false instead would mean you want to overwrite the file.
An example for your case:
try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("C:/YourEpicPath/ThisFileNeedsSomeAppending.txt", true)))) {
out.println("/sdcard/Docs/MyData");
}catch (IOException e1) {
//exception handling left as an exercise for the reader
}
Here is some documentation if you need for android, normally there shouldn't be any big differences.
Related
This question already has answers here:
Create a new line in Java's FileWriter
(10 answers)
Closed 1 year ago.
I have tried \n and stuff its still not working..?
I have also tried System.lineSeparator(); it still does not work. I have reviewed other posts.
FileWriter myWriter = new FileWriter("Random.txt");
myWriter.write(saltStr);
myWriter.close();
Yes, I have looped the file writer.
Output file:
2TFPXDLDKSFZUCIP5FGUAXZU
Just add new line symbol:
myWriter.write("\n");
Or system dependent from System.lineSeparator(), but \n will always work.
This question already has answers here:
How do I check if a file exists in Java?
(19 answers)
Closed 2 years ago.
I have created an executable jar file. User is required to put in his/her name and save it in order to proceed. This requires creation of a file, but due to *.txt at gitignore it will not be stored in git. I would like to know the proper way to create a file, so that user will be able to put in his name and proceed. What should I add besides:
File yourFile = new File("Name.txt");
yourFile.createNewFile();
FileOutputStream oFile = new FileOutputStream(yourFile, false);
try {
BufferedWriter reader = new BufferedWriter(new FileWriter(new File("Name.txt"), true));
reader.write(Data);
reader.newLine();
reader.close();
} catch (IOException E) {
System.out.println("Error is " + E);
}
You usually store and manage code in git. You usually don't store binaries and runtime data in git.
To my eye the code is lacking checking the existence if the file already exists. Please look here for more info: How do I check whether a file exists without exceptions?
By the way. Please don't do things like:
BufferedWriter reader (...)
It's not a reader. It's a writer.
This question already has answers here:
How to append text to an existing file in Java?
(31 answers)
Closed 7 years ago.
I am try to write data to a text file. I am using this code:
try (Writer writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("filename.txt"), "utf-8"))) {
writer.write("something");
}
But while the program is running, the file is overwriting the exist data that are already found in the text file. How can i write new data lines to the same text file without overwriting it ? Is there any easy and simple way to write it?
try (Writer writer = Files.newBufferedWriter(
Paths.get("filename.txt"), StandardCharsets.UTF_8,
StandardOpenOption.WRITE,
StandardOpenOption.APPEND)) {
writer.write("something");
}
The open options are a varargs list, and default to new file creation.
BTW FileWriter uses the platform encoding, so you where right to not use that class. It is not portable.
I think you may use FileWriter(File file, Boolean append)
Constructs a FileWriter object given a File object. If the second
argument is true, then bytes will be written to the end of the file
rather than the beginning.
This question already has answers here:
Why doesn't java.io.File have a close method?
(6 answers)
Closed 8 years ago.
After upgrading to Java 7 I get the following code flagged by Eclipse:
try (File file = new File(FILE_NAME)) {
file.delete();
}
Error is:
The resource type File does not implement java.lang.AutoCloseable
And Java's documentation doesn't have File listed in the AutoCloseable docs:
http://docs.oracle.com/javase/8/docs/api/java/lang/AutoCloseable.html
So besides adding the catch block, what is the suggested alternative?
As Jeffrey said in the comment to the question, you need to differentiate between a File and an InputStream, e.g. FileInputStream.
There is nothing to close in a File, but there is something to close in a stream or a reader.
try (FileInputStream fs = new FileInputStream (new File(FILE_NAME))) {
// do what you want with the stream
}
This question already has answers here:
Clear contents of a file in Java using RandomAccessFile
(2 answers)
Closed 9 years ago.
I am trying to clear the contents of a file I made in java. The file is created by a PrintWriter call. I read here that one can use RandomAccessFile to do so, and read somewhere else that this is in fact better to use than calling a new PrintWriter and immediately closing it to overwrite the file with a blank one.
However, using the RandomAccessFile to clear the file seems to be adding a string of null characters to the file (or perhaps it is the PrintWriter?) It only occurs if more text is added.
PrintWriter writer = new PrintWriter("temp","UTF-8");
while (condition) {
writer.println("Example text");
if (clearCondition) {
writer.flush();
new RandomAccessFile("temp","rw").setLength(0);
// Although the solution in the link above did not include ',"rw"'
// My compiler would not accept without a second parameter
writer.println("Text to be written onto the first line of temp file");
}
}
writer.close();
Running the equivalent of the above code is giving my temp file the contents:
^#^#^#^#^#^#^#^#^#^#^#^#^#Text to be written onto the first line of temp file
The number of null characters is equal to the number of characters erased (including newline characters). If no new text is added to the file, it is completely blank.
NOTE: writer needs to be able to write "Example Text" to the file again after the file is cleared. The clearCondition does not mean that the while loop gets broken.
EDIT: I have no idea what caused those null characters. I realized I am stupid and there was no need to have a temp file, just a temp String with all the data that would later be written to a file. Strings are super easy to reset with = ""; Thanks for all the suggestions
It doesn't seem a good idea to have an opened PrintWriter on the file and use a RandomAccessFile at the same time.
If you close your writer and open a new one on the file (instead of using RandomAccessFile) I think it will suit your needs.