package com.javasleep;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamWriteFile {
public static void main(String[] args) {
FileOutputStream fop = null;
File file;
String content = "写一些内容";
String path = "c:/javasleep/";
String fileName = "newfile.txt";
try {
file = new File(path);
//如果没有目录则创建
if(!file.exists()){
file.mkdirs();
}
file = new File(path+fileName);
fop = new FileOutputStream(file);
// 如果没有该文件则创建
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();
} finally {
try {
if (fop != null) {
fop.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamWriteFile {
public static void main(String[] args) {
FileOutputStream fop = null;
File file;
String content = "写一些内容";
String path = "c:/javasleep/";
String fileName = "newfile.txt";
try {
file = new File(path);
//如果没有目录则创建
if(!file.exists()){
file.mkdirs();
}
file = new File(path+fileName);
fop = new FileOutputStream(file);
// 如果没有该文件则创建
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();
} finally {
try {
if (fop != null) {
fop.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}