I'm using FileWrite class to write into a file.and its working fine. But FindBugs is pointing me a Minor issue in my code snippet.
code snippet:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd");
Date now = new Date();
String fileName = formatter.format(now) + ".txt";
FileWriter writer = null;
try {
File root = new File(Environment.getExternalStorageDirectory(), "Test");
if (!root.exists()) {
root.mkdirs();
}
File gpxfile = new File(root, fileName);
writer = new FileWriter(gpxfile, true);
writer.append(text + "\n\n");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Findbug Report:
Reliance on default encoding Found reliance on default encoding: new java.io.FileWriter(File, boolean)
In which line i'm getting this Error?
writer = new FileWriter(gpxfile, true);
Could some one please brief me what is this exactly? And how can we solve this?
java.io.FileWriter. Writes text to character files using a default buffer size. Encoding from characters to bytes uses either a specified charset or the platform's default charset. Whether or not a file is available or may be created depends upon the underlying platform.
setProperty("file. encoding", "UTF-8"); byte inbytes[] = new byte[1024]; FileInputStream fis = new FileInputStream("response. txt"); fis. read(inbytes); FileOutputStream fos = new FileOutputStream("response-2.
StreamWriter(Stream) Initializes a new instance of the StreamWriter class for the specified stream by using UTF-8 encoding and the default buffer size.
DM_DEFAULT_ENCODING: Reliance on default encoding Found a call to a method which will perform a byte to String (or String to byte) conversion, and will assume that the default platform encoding is suitable. This will cause the application behaviour to vary between platforms.
Resolved this Issue by replacing
FileWriter writer = new FileWriter(gpxfile, true);
with
FileOutputStream fileStream = new FileOutputStream(gpxfile);
writer = new OutputStreamWriter(fileStream, "UTF-8");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With