Java基础知识总结(66)

**

  • FileOutputStream练习 */ public class FileOutputStreamDemo { public static void main(String[] args) {

    String path = "D:\\IoDemo\\test2.txt";
    //如果文件不存在,则自动创建
    //append:是指是否在原有内容后追加,默认为FALSE
    try(OutputStream os = new FileOutputStream(path)){
        char c = '赞';
        int a = c;
        os.write(a);
    }catch (FileNotFoundException e) {
        e.printStackTrace();
    }catch (IOException e){
        e.printStackTrace();
    }

    } }

/**

  • FileOutputStream练习 */ public class FileOutputStreamDemo2 {

    public static void main(String[] args) { String path = "D:\IoDemo\test2.txt"; //如果文件不存在,则自动创建 //append参数为true:在原有内容后追加 try(OutputStream os = new FileOutputStream(path,true)){ String s = "众里寻他千百度,蓦然回首,那人却在,灯火阑珊处。 "; os.write(s.getBytes()); }catch (IOException e){ e.printStackTrace(); } } }

    同时使用输入流和输出流来复制文件

/**

  • 同时使用FileInputStream和FileOutputStream完成文件的复制操作 */ public class CopyUtilDemo { public static void main(String[] args) { File src = new File("D:\SSH.zip"); File dest = new File("D:\IoDemo\ssh.zip"); copy(src,dest); } public static void copy(File src, File dest){ long start = System.currentTimeMillis(); try(InputStream inputStream = new FileInputStream(src); OutputStream outputStream = new FileOutputStream(dest)) { byte[] bytes = new byte[16384]; int k =0; while((k=inputStream.read(bytes))!=-1){ outputStream.write(bytes); } } catch (IOException e) { e.printStackTrace(); } long end = System.currentTimeMillis(); System.out.println("复制完成!"); System.out.println("共用时"+(end-start)/1000+"秒"); } }

/**

  • 解压文件练习 */ public class ZipOutputStreamDemo { public static void main(String[] args) { try{ InputStream inputStream = new FileInputStream("D:\IoDemo\test.zip"); ZipInputStream zipInput = new ZipInputStream(inputStream, Charset.forName("gbk")); ZipEntry zipEntry = null; while ((zipEntry = zipInput.getNextEntry())!=null){ String name = zipEntry.getName(); System.out.print(name); int k = 0; OutputStream outputStream = new FileOutputStream("D:\IoDemo\"+name); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream); while((k = zipInput.read())!=-1){ bufferedOutputStream.write(k); } bufferedOutputStream.close(); outputStream.close(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { throw new RuntimeException(e); } } }

/**

  • 压缩文件练习 */ public class ZipInputStreamDemo { public static void main(String[] args) throws IOException { File file = new File("D:\IoDemo\Typora 1.2.3.exe"); FileInputStream fileInputStream = new FileInputStream(file); OutputStream outputStream = new FileOutputStream("D:\IoDemo\typora.zip"); ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream); zipOutputStream.putNextEntry(new ZipEntry(file.getName())); int k; while ((k = fileInputStream.read())!=-1){ zipOutputStream.write(k); } zipOutputStream.close(); outputStream.close(); fileInputStream.close(); } } 2、今天没学会什么

学习心得

今天主要学习了I/O流,重点为字节流的输入输出,练习了常用的方法。通过练习更好的掌握主要的知识点,在实践中积累使用经验。

课后练习:

  1. 遍历D盘,将D盘中所有的路径名和文件名写入文本文件中

/**

  • 遍历D盘,将D盘中所有的路径名和文件名写入文本文件中 */ public class IoDemo2 { public static void main(String[] args) { String path = "D:\"; File file = new File(path); ArrayList<String> filenames = new ArrayList<>(); filenames = getPathFileName(file,filenames); generateTxt(filenames); }

    /**

    • 获取D盘下所有的路径名和文件名

    • @param baseFile */ private static ArrayList<String> getPathFileName(File baseFile,ArrayList<String> fileName) { //File对象的索引子文件和路径 File[] files = baseFile.listFiles(); //在files对象不为Null时遍历 ArrayList<String> filenames = fileName; if (files != null && files.length > 0) { //遍历路径和文件列对象 for (File file : files) { //判断是否是文件 if (file.isFile()) { //调用方法写入 filenames.add(file.getAbsolutePath()); } //判断是否是路径 if (file.isDirectory()) { //如果是路径,需要继续递归调用方法并传递文件对象参数继续遍历输出文件和路径 getPathFileName(file,filenames); }

      }

      } return filenames; }

    private static void generateTxt(ArrayList<String> files){ String path = "D:\IoDemo\filename.txt"; //如果文件不存在,则自动创建 //append参数为true:在原有内容后追加 //资源自动释放 try(OutputStream os = new FileOutputStream(path,true)){ //遍历ArrayList for (int i = 0; i < files.size(); i++) { //将数据写入 os.write(files.get(i).getBytes()); os.write("\r\n".getBytes()); } }catch (IOException e){ e.printStackTrace(); } } }

  • 11
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

好教员好

您的鼓励是我前进动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值