import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class WriteFileExample2 {
public static void main(String[] args) {
File file = new File("d:/java4.txt");
String content = "This is the text content";
//New jdk 1.7 feature
try (FileOutputStream fop = new FileOutputStream(file)) {
// if file doesn't exists, then create it
if (!file.exists()) {
file.createNewFile();
}
// Get the content in bytes
byte[] contentInBytes = content.getBytes();
fop.write(contentInBytes);
fop.flush();
fop.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.FileOutputStream;
import java.io.IOException;
public class WriteFileExample2 {
public static void main(String[] args) {
File file = new File("d:/java4.txt");
String content = "This is the text content";
//New jdk 1.7 feature
try (FileOutputStream fop = new FileOutputStream(file)) {
// if file doesn't exists, then create it
if (!file.exists()) {
file.createNewFile();
}
// Get the content in bytes
byte[] contentInBytes = content.getBytes();
fop.write(contentInBytes);
fop.flush();
fop.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}
}
No comments:
Post a Comment