天天看点

创建文件夹、创建文件、拷贝文件

创建文件夹

public static void createFolder(String folderPath) {

try {

File filePath = new java.io.File(folderPath);

if (!filePath.exists()) {

filePath.mkdir();

}

} catch (Exception e) {

System.out.println("error in createFolder");

e.printStackTrace();

}

}

创建文件

public static void createFile(String filePathName, String content) {

try {

File filePath = new File(filePathName);

if (!filePath.exists()) {

filePath.createNewFile();

}

FileWriter resultFile = new FileWriter(filePath);

PrintWriter newFile = new PrintWriter(resultFile);

newFile.println(content);

resultFile.close();

} catch (Exception e) {

System.out.println("error in createFile");

e.printStackTrace();

}

}

拷贝文件

public static void copyFile(String oldPath, String newPath) {

try {

int bytesum = 0;

int byteread = 0;

File oldfile = new File(oldPath);

if (oldfile.exists()) {

InputStream inStream = new FileInputStream(oldPath);

FileOutputStream fs = new FileOutputStream(newPath);

byte[] buffer = new byte[9999];

while ((byteread = inStream.read(buffer)) != -1) {

bytesum += byteread;

System.out.println(bytesum);

fs.write(buffer, 0, byteread);

}

inStream.close();

}

} catch (Exception e) {

System.out.println("");

e.printStackTrace();

}

}

继续阅读