1. 程式人生 > 其它 >面試 ---- i++在兩個執行緒中分別執行100次,最大值和最小值分別是多少?

面試 ---- i++在兩個執行緒中分別執行100次,最大值和最小值分別是多少?

位元組流   
輸入流FileInputStream,讀檔案
輸出流FileOutputStream,寫檔案

FileInputStream inputStream=new FileInputStream(srcFile);
FileOutputStream outStream=new FileOutputStream(destFile);


位元組緩衝輸入流:BufferedInputStream bit=new BufferedInputStream(new FileInputStream(srcFile));

位元組緩衝輸出流:BufferedOutputStream bot=new BufferedOutputStream(new FileOutputStream(destFile));
package task;

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;

public class TaskIo {
public  static void  copyFile(File srcFile) throws
IOException { File destFile=new File("e:\\b",srcFile.getName());//此處修改目標資料夾 FileInputStream inputStream=new FileInputStream(srcFile); FileOutputStream outStream=new FileOutputStream(destFile); byte[] bytes=new byte[1024]; int len=0; while((len=inputStream.read(bytes))!=-1) { outStream.write(bytes,
0,len); } inputStream.close(); outStream.close(); } public static void copyFolder(File srcFile) throws IOException { File[] files=srcFile.listFiles(); for(File f:files) { if(f.isDirectory()) { copyFolder(f); }else { copyFile(f); } } } public static void deleteFile(File srcFile) { File[] files=srcFile.listFiles(); for(File f:files) { if(f.isDirectory()) { deleteFile(f); }else { f.delete(); } } } public static void deleteFile(String srcPath) { File src=new File(srcPath); File[] files=src.listFiles(); for(File f:files) { if(f.isDirectory()) { deleteFile(f); }else { f.delete(); }src.delete();//複製到新資料夾後,把原來路徑總資料夾也刪了 } } public static void main(String[] args) throws IOException { // TODO Auto-generated method stub //把e盤a資料夾下的tiku.txt檔案,複製到b資料夾裡,複製一個檔案 // FileInputStream stream=new FileInputStream("E:\\a\\tiku.txt"); // FileOutputStream outStream=new FileOutputStream("E:\\b\\tiku.txt"); // byte[] bytes=new byte[1024]; // int len=0; // while((len=stream.read(bytes))!=-1) { // outStream.write(bytes,0,len); // } // stream.close(); // outStream.close(); // 把e盤a資料夾下的所有檔案,複製到b資料夾裡,b資料夾必須存在 // File srcFile=new File("e:\\a"); // copyFolder(srcFile); // 把e盤a資料夾下的所有檔案,剪下到b資料夾裡,b資料夾必須存在 // File srcFile=new File("e:\\a"); // copyFolder(srcFile); // deleteFile(srcFile); // 複製指定資料夾到指定目錄,指定目錄可能不存在 // String srcPath="e:\\b"; // String destPath="e:\\a"; // copyDir1(srcPath,destPath); // deleteFile(srcPath); // 複製的檔案內容很大,分別用讀取一組位元組,位元組緩衝流讀取一組位元組,看哪個快 // String srcPath="e:\\java\\視訊"; // String destPath="e:\\b"; // long a=System.currentTimeMillis(); // copyDir1(srcPath,destPath); // long b=System.currentTimeMillis(); // System.out.println(b-a); // 74965 // String srcPath="e:\\java\\視訊"; // String destPath="e:\\c"; // long a=System.currentTimeMillis(); // copyDir2(srcPath,destPath); // long b=System.currentTimeMillis(); // System.out.println(b-a); // 52310 } public static void copyDir1(String srcPath,String destPath) throws IOException { File src=new File(srcPath); File dest=new File(destPath); File[] srcFiles=src.listFiles(); if(!dest.exists()) { dest.mkdirs(); } for(File srcFile:srcFiles) { if(srcFile.isDirectory()) { String dirName=srcFile.getName(); File newDestDir=new File(dest,dirName); //遞迴,用來複制原始檔的所有資料夾(不含檔案) copyDir1(srcFile.getPath(),newDestDir.getPath()); }else { String destName=srcFile.getName(); File destFile=new File(dest,destName); copyFile1(srcFile,destFile); } } } public static void copyDir2(String srcPath,String destPath) throws IOException { File src=new File(srcPath); File dest=new File(destPath); File[] srcFiles=src.listFiles(); if(!dest.exists()) { dest.mkdirs(); } for(File srcFile:srcFiles) { if(srcFile.isDirectory()) { String dirName=srcFile.getName(); File newDestDir=new File(dest,dirName); //遞迴,用來複制原始檔的所有資料夾(不含檔案) copyDir2(srcFile.getPath(),newDestDir.getPath()); }else { String destName=srcFile.getName(); File destFile=new File(dest,destName); copyFile2(srcFile,destFile); } } } public static void copyFile1(File srcFile,File destFile) throws IOException { FileInputStream inputStream=new FileInputStream(srcFile); FileOutputStream outStream=new FileOutputStream(destFile); byte[] bytes=new byte[1024]; int len=0; while((len=inputStream.read(bytes))!=-1) { outStream.write(bytes,0,len); } inputStream.close(); outStream.close(); } public static void copyFile2(File srcFile,File destFile) throws IOException { BufferedInputStream bit=new BufferedInputStream(new FileInputStream(srcFile)); BufferedOutputStream bot=new BufferedOutputStream(new FileOutputStream(destFile)); byte[] bytes=new byte[1024]; int len=0; while((len=bit.read(bytes))!=-1) { bot.write(bytes,0,len); } bit.close(); bot.close(); } }