Writing to text file, file not updating [duplicate] - java

This question already has answers here:
Writing String to Text File
(6 answers)
Closed 8 years ago.
I have been having some trouble: my previous question here explains it all. I was trying to write to a file in the external storage which on my device is /data/media or /sdcard. The file (when you adb pull it with device on) one saves two lines of text and then gets overwritten but once you adb pull it again in recovery with /data mounted, all the logs appear.
I have tried mounting /data and then writing to the file but still no luck... Any help?

You code does not flush the BufferedWriter, so data are not written to log file but stays in the buffer.
How about replace code in try block of method 'writeToLog' of your code by following code?
BufferedWriter bw = new BufferedWriter(new FileWriter(logFile, true))
PrintWriter out = new PrintWriter(bw);
out.println(text);
bw.flush() // Explicitly flushbufferedWriter
out.close();

Related

How to save all the System.out.println present in the code in a .txt file? [duplicate]

This question already has answers here:
System.out.println to text file
(3 answers)
Closed 2 years ago.
Running my code, I would like to save all the System.out.println() in a file.
For example:
System.out.println("Save this!");
I would need a file .txt in which is stored the string "Save this!".
Someone could help me?
Regards,
Francesco Campanile
Use this to Write console to a file.
PrintStream out = new PrintStream(
new FileOutputStream("output.txt", true), true);
System.setOut(out);
The output file will be on your project's root directory

give read write permission to file [duplicate]

This question already has answers here:
How do I programmatically change file permissions?
(12 answers)
Closed 6 years ago.
hello guys I want to give file permission to open in read mode or write mode
.ext contains file extention and file_name contains name of file. f_p is a veriable where I an geting input as 'r' or 'w' mode. Here I am using same file at different locations
But in this code I am getting error as
cannot find symbol: method setReadable(boolean)
location: fos2 is of type FileOutputStream
<%
some code here
FileInputStream fis2 = new FileInputStream("e:/profile/epy/"+file_name+".ext");
FileOutputStream fos2 = new FileOutputStream("e:/decrypt/"+file_name+"."+ext);
if(f_p.equals("R")||f_p.equals("r"))
{
fos2.setReadable(true);
}
else if(f_p.equals("W")||f_p.equals("w"))
{
fos2.setWritable(true);
}
// some code here
%>
https://jsfiddle.net/wc8pccyL/
The current code uses the wrong class (FileOutputStream).
File f = new File(SOME_PATH);
if ("r".equalsIgnoreCase(f_p)) {
f.setReadable(true);
...
}
if ("w".equalsIgnoreCase(f_p)) {
f.setWritable(true);
...
}
However, one should be careful in assuming that one would want write access without read access. The assumption in the OP's code is that the f_p has a single value of "R" or "W", and sets the permission. This assumption should be carefully checked, especially across operating systems.
Also, if the FileOutputStream has to be used later (for actual output), it has a constructor that takes a File object, so there is nothing lost by creating the File object in such a scenario, and then creating the FileOutputStream fos = new FileOutputStream(f); where 'f' is the previously instantiated File object.

i want to display these two values in a text file [duplicate]

This question already has answers here:
How do I save a String to a text file using Java?
(24 answers)
Closed 6 years ago.
I have two string variables containing some user input.
String name="neil";
String mobile="5555";
now I want to display neil an 5555 in a text file. Please help me to display this in a text file, I know how to display content of a file which is already exist, but donot know how to work with this case, please help me...
PrintWriter out = response.getWriter();
This should be changed to use "java.io.File" class.
Has been explained in this thread: How to use PrintWriter and File classes in Java?
File file = new File("C:/Users/Me/Desktop/directory/file.txt");
file.getParentFile().mkdirs();
PrintWriter out = new PrintWriter(file);
Something like this.
ps: check out "try-with-resources"

Writing from console to text file, file data not saved

I am trying to save the content of the java console into a text file but each time I close the program the text file goes blank and rewrites to it. i.e. if I write to a file today, close the program and come back and run it again tomorrow, it has remembered the information written to it.
You want to open the OutputStream in append mode. Demo code:
PrintWriter out = new PrintWriter(
new FileOutputStream(new File(filename), true));
What you experience is the normal behavior when you write a stream to a file, and this is not specific to the Java API.

Best way in Java to write in the middle of a file [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Best Way to Write Bytes in the Middle of a File in Java
I'm writing a program that modifies a PostScript file to add print properties, so I need to add lines in the middle of the file. These PostScript files are very large, and I want to know if the code I'm using is the most efficient. This code reads the source file and writes a temporary file adding a line where is needed.
Writer out = null;
Scanner scanner = null;
String newLine = System.getProperty("line.separator");
scanner = new Scanner(new FileInputStream(fileToRead));
out = new OutputStreamWriter(new FileOutputStream(fileToWrite));
String line;
while(scanner.hasNextLine()){
line = scanner.nextLine();
out.write(line);
out.write(newLine);
if(line.equals("%%BeginSetup")){
out.write("<< /Duplex true /Tumble true >> setpagedevice");
out.write(newLine);
}
}
scanner.close();
out.close();
Any help will be appreciated, thanks in advance.
Most of the old answers found on SO uses/links the old java.io.*
Oracle has nice examples how to do this using the "new" java 7 java.nio.* packages (usually with much better performance)
http://docs.oracle.com/javase/tutorial/essential/io/rafs.html
See RandomAccessFile and its example here: Fileseek - You can seek to a particular location in the file and write to it there.

Categories

Resources