By going through this documentation, It states that:
Deprecated. 2.5 use writeStringToFile(File, String, Charset) instead
Writes a String to a file creating the file if it does not exist using the default encoding for the VM.
You can follow this example:
final File file = new File(getTestDirectory(), "file_name.txt");
FileUtils.writeStringToFile(file, "content", "ISO-8859-1");
You can pass (String) null in your argument if you don't want to pass the encoding.
For more information, you can go through the link: Usage link
Answer from Rajendra arora on Stack Overflowjava - What is the alternative to deprecated the FileUtils.writeStringToFile method? - Stack Overflow
java - import org.apache.commons.io.FileUtils - Software Quality Assurance & Testing Stack Exchange
java - FileUtils.listFiles() on resources directory - Stack Overflow
java - Using FileUtils in eclipse - Stack Overflow
Videos
By going through this documentation, It states that:
Deprecated. 2.5 use writeStringToFile(File, String, Charset) instead
Writes a String to a file creating the file if it does not exist using the default encoding for the VM.
You can follow this example:
final File file = new File(getTestDirectory(), "file_name.txt");
FileUtils.writeStringToFile(file, "content", "ISO-8859-1");
You can pass (String) null in your argument if you don't want to pass the encoding.
For more information, you can go through the link: Usage link
Use this one
File file = ...
String data = ...
try{
FileUtils.writeStringToFile(file, data, Charset.defaultCharset());
}catch(IOException e){
e.printStackTrace();
}
I have a question about Apache Commons IO with Java.
The excercice is to read a text file without using Commons IO, and then using it.
For the first one, my code is this, and this works:
import java.util.Scanner;
public class ReadBook {
private Scanner line;
public void openFile(){
try{
line= new Scanner(new File("books.txt"));
}
catch(Exception e){
System.out.println("File not found");
}
}
public void readFile() {
while (line.hasNext()) {
String book = line.next();
System.out.println(book);
}
}
public void closeFile(){
line.close();
}
}
and the main class:
public class mainClass {
public static void main(String[] args){
ReadBook book = new ReadBook();
book.openFile();
book.readFile();
book.closeFile();
}
}
The teacher gave us this class, and now we are supposed to do the same thing with the Apache Commons IO library.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
public class Utf8File {
public static String loadFileIntoString(String filePath) throws FileNotFoundException, IOException {
return IOUtils.toString(new FileInputStream(filePath), "UTF-8");
}
public static void saveStringIntoFile(String filePath, String content) throws IOException {
File f = new File(filePath);
FileUtils.writeStringToFile(f, content, "UTF-8");
}
}I was wondering, do you know how to do it?
Last time I programmed with Java was one year ago hahaha so it is a little bit far away in my memory :O
This is the first homework to do (today was the first class).
I hope someone can help me :)
Thanks!