package cn.file;
import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;
/***@作者:木鹤辞
*@创建时间:2020年11月19日下午2:47:10
*@描述:
*代码实现在D盘下创建3个文件夹D:\\you\\me\\this
*分别在3个文件夹下创建3个you.txt me.txt this.txt
*再给每个文件中写入一首歌
*修改文件名为对应的歌名
*最后再给3个文件夹下复制对应的歌曲mp3格式的
*/
public class CHS { public static void main(String[] args) throws Exception { String path = "D:\\";
String[] str_dir = {"me","you","this"}; for (int i = 0; i < str_dir.length; i++) {
if (!new File(path+str_dir[i]).isDirectory()) { craDir(path+str_dir[i]);
}
}
String[] str_file = {"\\me.txt","\\you.txt","\\this.txt"}; for (int i = 0; i < str_file.length; i++) {
if (!new File(path+str_dir[i]+str_file[i]).isFile()) { craFile(path+str_dir[i]+str_file[i]); } }
String[] str_geci = {"me1","you1","this1"};
for (int i = 0; i < str_file.length; i++) { mywriter(path+str_dir[i]+str_file[i],str_geci[i]); }
String[] str_mp3 = {"\\me1.txt","\\you1.txt","\\this1.txt"};
for (int i = 0; i < str_mp3.length; i++) { copeMP3(path+str_dir[i]+str_file[i],path+str_dir[i]+str_mp3[i]); } }
//创建多级文件夹
public static void craDir(String path) {
//1.创建文件对象
File f = new File(path);f.mkdirs(); } //创建文件
public static void craFile(String path) throws IOException {
//1.创建文件对象
File f = new File(path); f.createNewFile(); }
//写入歌曲
public static void mywriter(String path,String geci) throws Exception { FileOutputStream fos = new FileOutputStream(path); fos.write(geci.getBytes()); fos.close(); }
//复制mp3歌曲 public static void copeMP3(String path,String path1) throws Exception {
FileInputStream fis = new FileInputStream(path); BufferedInputStream bis = new BufferedInputStream(fis); FileOutputStream fos = new FileOutputStream(path1); BufferedOutputStream bos = new BufferedOutputStream(fos);
int lan =0;
while ((lan=bis.read())!=-1) { bos.write(lan);
}
bis.close();
fis.close();
bos.close();
fos.close();
if(new File(path1).isFile()) { System.out.println("复制完成");
} }}
文件字符流
最新推荐文章于 2023-01-17 09:48:40 发布