java 同时创建多个文件夹和文件


	public void demo1() {
		try {
			File dir = new File("d:\\abc\\bcd");
			if (!dir.exists()) {
				dir.mkdirs();
			}
			File file = new File(dir, "demo.txt");
			if (!file.exists()) {
				file.createNewFile();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
 
	
	public void demo2() {
		File file = new File("c:\\1\\2\\3\\demo.txt");
		File dir = file.getParentFile();
		if (!dir.exists()) {
			dir.mkdirs();
		}
		try {
			file.createNewFile();
		} catch (IOException e) {
			e.printStackTrace();
		}

        file.getAbsolutePath();//获取文件的绝对路径
	}


    //读写文件
    FileInputStream fis = new FileInputStream("aaaa.txt");
    BufferedInputStream bufis = new BufferedInputStream(fis);
    
    FileOutputStream fos = new FileOutputStream("aaaa_copy.txt");
    BufferedOutputStream bufos = new BufferedOutputStream(fos);
    
    byte[] buf = new byte[1024];
    
    int by = 0;
    while((by = bufis.read(buf))!=-1){
        bufos.write(buf,0,by);
        bufos.flush();
    }
    
    fos.close();
    fis.close();

 

你可能感兴趣的:(java)