`

java 文件复制

 
阅读更多
/**
	 * 复制文件操作
	 * 路径可以用相对路径 ,也可以用绝对路径
	 * @param sourceFile 源文件路径
	 * @param newFile    新文件路径
	 * */
	public static void copyFile(String sourceFile,String newFile){
		File img1 = new File(sourceFile);//源文件
		File copyimg = new File(newFile);//复制的新文件
		if(!copyimg.exists()){//如果文件不存在 创建
			try {
				copyimg.createNewFile();
			} catch (IOException e) {
				System.out.println("copyimg.creatNewFile() error!");
			}
		}
		FileOutputStream fos = null;
		FileInputStream  fis = null;
	    byte[] buffer = null;
	    try {//创建文件读写流
			fos = new FileOutputStream(copyimg);
		    fis = new FileInputStream(img1);
		} catch (FileNotFoundException e) {
			System.out.println("new stream error!");
		}
	     try {//创建 byte数组
			buffer = new byte[fis.available()];
		} catch (IOException e) {
		}
		try {//文件流读写
			fis.read(buffer);
			fos.write(buffer);
		} catch (IOException e) {
			System.out.println("fis fos error!");
		}
		try {//关闭文件流
			fis.close();
			fos.close();
		} catch (IOException e) {
			System.out.println("close error!");
		}
	}

		File file = new File("C:\\Users\\mumaqm\\Desktop\\bbb.txt");
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;
			while ((tempString = reader.readLine()) != null) {
				System.out.print(tempString+",");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics