Executing new file(testResultFile).canWrite(); will provide a Boolean value indicating whether the file is writable or not. This method should be utilized for checking writability.
boolean bool = new file(testResultFile).canWrite();
To write content into a file, you can follow this approach:
FileOutputStream fop = null;
File file;
String content = "This is the text content";
try {
file = new File("c:/newfile.txt");
fop = new FileOutputStream(file);
// create the file if it doesn't exist
if (!file.exists()) {
file.createNewFile();
}
// convert content to bytes
byte[] contentInBytes = content.getBytes();
fop.write(contentInBytes);
fop.flush();
fop.close();
System.out.println("Done");
} catch (Exception e) {
e.printStackTrace();
}
Here is an alternative method based on a comment:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
public class PrintTest {
public static void main(String[] args) throws FileNotFoundException {
File file=new File("C:\\CopyMessageTest.txt");
boolean bool = file.canWrite();
if(bool==false){
file.setWritable(true);
}
FileOutputStream fis = new FileOutputStream(file);
PrintStream out = new PrintStream(fis);
System.setOut(out);
System.out.println("----------Sucessfully Logged In ----------------");
}
}
Thank You,
Murali