当前位置:编程学习 > JAVA >>

javaWeb学习之旅(一)------------java基础知识增强

一、java基础
1.文件的复制
debug调试
F5: step into
F6:  step over
F7:  step return

思路如下:

 


java实现的方法如下


[java]
public class Demo1 { 
 
    @Test 
    public static void main(String[] args) { 
        String file1 = "F:\\test1.txt"; 
        String file2 = "F:\\test.txt"; 
        try { 
            // 复制  
            copy(file1, file2); 
            System.out.println("文件2拷贝成功"); 
        } catch (Exception e) { 
            // TODO: handle exception  
        } 
 
    } 
 
    private static void copy(String file1, String file2) throws Exception { 
        // 以流的形式读入文件1中的内容  
        InputStream inputStream = readFile(file1); 
        writetoFile(inputStream, file2); 
    } 
 
    private static void writetoFile(InputStream inputStream, String file2) 
            throws IOException { 
        // TODO Auto-generated method stub  
        // 如果文件2不存在,则创建文件2  
        if (!existFile(file2)) { 
            File file = new File(file2); 
            file.createNewFile(); 
        } 
        // 打开文件2 的输出流  
        FileOutputStream fileOutputStream = new FileOutputStream(file2); 
        intout(inputStream, fileOutputStream); 
 
    } 
 
    // 从buffer中读入并写入文件2中  
    private static void intout(InputStream inputStream, 
            FileOutputStream fileOutputStream) { 
        try { 
            int len = 0; 
            byte buffer[] = new byte[1024]; 
            while ((len = inputStream.read(buffer)) > 0) { 
 
                fileOutputStream.write(buffer, 0, len); 
 
            } 
 
        } catch (Exception e) { 
            // TODO: handle exception  
        } finally { 
            if (inputStream != null) { 
                try { 
                    inputStream.close(); 
                } catch (IOException e) { 
                    // TODO Auto-generated catch block  
                    e.printStackTrace(); 
                } 
            } 
            if (fileOutputStream != null) { 
                try { 
                    fileOutputStream.close(); 
                } catch (IOException e) { 
                    // TODO Auto-generated catch block  
                    e.printStackTrace(); 
                } 
            } 
 
        } 
 
        // TODO Auto-generated method stub  
 
    } 
 
    // 读文件  
    private static InputStream readFile(String file1) 
            throws FileNotFoundException { 
        // TODO Auto-generated method stub  
 
        // 判断文件1存在  
        if (!existFile(file1)) { 
            throw new FileNotFoundException("指定的文件不存在"); 
        } 
 
        // 向输入流中读文件  
        FileInputStream inputStream = new FileInputStream(file1); 
 
        return inputStream; 
    } 
 
    // 判断文件是否存在  
    private static boolean existFile(String existfile) { 
        // TODO Auto-generated method stub  
 
        File file = new File(existfile); 
        if (file.exists()) { 
            return true; 
        } 
        return false; 
    } 
 

public class Demo1 {

 @Test
 public static void m

补充:Web开发 , 其他 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,