打印流:

  有两个类:PrintStream     PrintWriter类,两个类的方法一样,构造方法不一样

  PrintStream构造方法:接收File类型,接收字符串文件名,接收字节输出流(OutputStream)

  PrintWriter构造方法:接收File类型,接收字符串文件名,接收字节输出流(OutputStream),接收字符输出流(Writer)

为其他流添加功能,可以方便的打印各种数据值,不同的是,他永远不会抛出IO异常

package com.zs.Demo2;

import java.io.*;

public class Demo {
public static void main(String[] args) {
try {
fun1();
fun2();
} catch (IOException e) {
e.printStackTrace();
}
} private static void fun2() throws IOException {
FileWriter f=new FileWriter("h:\\a.txt");
//PrintWriter接收字节输出流,也接受字符输出流
PrintWriter p=new PrintWriter(f);
p.write("hello java");
p.close();
} private static void fun1() throws IOException {
FileOutputStream f=new FileOutputStream("h:\\a.txt");
//PrintStream只接收字节输出流,接收字符输出流时会报错
PrintStream p=new PrintStream(f);
p.write(101);//输出编码后的字符e
p.print(101);//输入什么就是输出什么
p.write("hello world\r\n".getBytes());
p.print("hello java");
p.close();
}
}

打印流复制文件,自动刷新

package com.zs.Demo2;

import java.io.*;

public class Demo2 {
public static void main(String[] args) {
try {
fun();
fun2();
} catch (IOException e) {
e.printStackTrace();
}
}
//打印流自动刷新
private static void fun2() throws FileNotFoundException {
//从下面可以看出,printWriter可以接收字节输出流
PrintWriter pw=new PrintWriter(new FileOutputStream("h:\\a.txt"),true);
//构造方法中第一个参数是文件类型,第二个参数true是是否自动刷新,不需要写flush方法
pw.write("hello world");
pw.close();
} //打印流复制文件
private static void fun() throws IOException {
BufferedReader fr=new BufferedReader(new FileReader("h:\\a.txt"));
// PrintWriter pw=new PrintWriter("h:\\b.txt",true);报错,开启自动刷新必须参数为File类型
PrintWriter pw=new PrintWriter(new FileWriter("h:\\b.txt"),true);
String s=null;
while ((s=fr.readLine())!=null) {
pw.write(s);
}
pw.close();
fr.close();
}
}

IO流操作工具类:commons工具类(在网上下载)

  它是一个操作IO流的类,可以简化代码,就像数组的操作类Arrays,集合操作类Collections类一样,这是IO流操作类,使用前要构建路径,在当前项目下新建lib文件夹,放入commons包,然后eclipse右击该包点击构架路径(buildpath),idea导包:项目下建lib文件夹,放入要导入的包,右击包,Add as library..

package com.zs;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils; import java.io.File;
import java.io.IOException; public class Demo3 {
public static void main(String[] args) {
try {
fun();//文件操作
fun2();//文件名操作
} catch (IOException e) {
e.printStackTrace();
}
} private static void fun2() {
//获得文件名
String name = FilenameUtils.getName("g:\\111\\a.txt");
System.out.println(name);//a.txt
//获得后缀,可以输入文件地址,
String name2 = FilenameUtils.getExtension(name);
System.out.println(name2);//txt
//判断文件后缀
boolean b = FilenameUtils.isExtension("c.java", "java");
System.out.println(b);//true } private static void fun() throws IOException {
//写字符串到文件
FileUtils.writeStringToFile(new File("h:\\a.txt"),"java");
//读文件内容
String s = FileUtils.readFileToString(new File("h:\\a.txt"));
System.out.println(s);
//复制文件夹
FileUtils.copyDirectory(new File("e:\\java.jar"),new File("h:\\赵帅\\java.jar"));
//复制一个文件夹到另一个文件夹,要复制到的文件夹不存在时,会新建文件夹
FileUtils.copyDirectoryToDirectory(new File("h:\\赵帅\\linux"),new File("e:\\cz"));
//复制文件
FileUtils.copyFile(new File("h:\\a.txt"),new File("g:\\a.txt")); }
}

Java学习笔记33(IO:打印流,IO流工具类)的更多相关文章

  1. 0025 Java学习笔记-面向对象-final修饰符、不可变类

    final关键字可以用于何处 修饰类:该类不可被继承 修饰变量:该变量一经初始化就不能被重新赋值,即使该值跟初始化的值相同或者指向同一个对象,也不可以 类变量: 实例变量: 形参: 注意可以修饰形参 ...

  2. Java学习笔记43(打印流、IO流工具类简单介绍)

    打印流: 有两个类:PrintStream,PrintWriter类,两个类的方法一致,区别在于构造器 PrintStream:构造方法:接收File类型,接收字符串文件名,接收字节输出流(Outpu ...

  3. java学习笔记(7)——I/O流

    一.File类 File(File parent, String child); File(Stirng filename); ------------------------------------ ...

  4. Java学习笔记33(集合框架七:Collections工具类)

    数组有工具类,方面操作数组 集合也有工具类:Collections 常用方法示例: package demo; import java.util.ArrayList; import java.util ...

  5. ArcGIS API for JavaScript 4.2学习笔记[26] 缓冲区分析【基于geometryEngine工具类】

    要说GIS空间分析最经典的例子,就是缓冲区分析了. 本例使用geometryEngine来绘制缓冲区环.因为官方给的例子有3D和2D场景,所以就会显得比较复杂. 当鼠标在视图上点击时,就会生成一个缓冲 ...

  6. Java学习笔记18---final关键字修饰变量、方法及类

    英语里final这个单词大家都知道是"最终的"意思,其实还有一个意思是"不可更改的".在Java里,final关键字作"不可更改的"来解释更 ...

  7. Java学习笔记44(多线程一:Thread类)

    多线程的概念:略 多线程的目的:提高效率 主线程: package demo; //主线程 public class Demo { public static void main(String[] a ...

  8. java学习笔记18(基本类型包装类,system类)

    基本类型包装类 定义:程序界面用户输入的数据都是以字符串类型存储的,如果需要操作这些字符串进行运算,需要转成基本数据类型,这时就要用到基本类型包装类,例: public class Demo { pu ...

  9. java学习笔记之基础篇

    java选择语句之switch   //switch可以用于等值判断 switch (e) //int ,或则可以自动转化成int 的类型,(byte char short)枚举jdk 7中可以防止字 ...

随机推荐

  1. 『Python × C++』函数传参机制学习以及对比

    一.Python函数传参 在python中,函数传参实际上传入的是变量的别名,由于python内在的变量机制(名称和变量值相互独立),只要传入的变量不可变(tuple中的元素也要是不可变的才行),那么 ...

  2. shiro中SSL

    对于SSL的支持,Shiro只是判断当前url是否需要SSL登录,如果需要自动重定向到https进行访问. 首先生成数字证书,生成证书到D:\localhost.keystore 使用JDK的keyt ...

  3. linux指令统计日志出现的次数

    cat  XXX.log|grep ''|grep '条件'| wc -l   单个条件统计 cat  XXX.log|grep ''|grep '条件1'|grep '条件2'|grep '条件3' ...

  4. 集成学习一: Bagging

    目录 偏倚与方差 Bagging 自助采样 投票 随机森林 参考文献: ''团结就是力量'' 对问题进行建模时, 算法无论如何优化都无法达到我们的要求,又或者精准算法的实现或调优成本太大, 这时,我们 ...

  5. [CodeForces - 447A] A - DZY Loves Hash

    A - DZY Loves Hash DZY has a hash table with p buckets, numbered from 0 to p - 1. He wants to insert ...

  6. Humble Numbers HDU - 1058 2分dp

    JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which are located in two ...

  7. Weblogic domain扩展教程

    weblogic扩展domain有两种扩展,一是机器上已有要扩展的domain只是在其内增加受管服务器,二是机器上没有domain要新建domain然后增加受管服务器 一.机器上已有要扩展的domai ...

  8. python格式化日期

    #!/usr/bin/python # -*- coding: UTF-8 -*- import time import calendar """ 时间元组(年.月.日. ...

  9. 阿里云免费申请https证书

    申请地址   https://common-buy.aliyun.com/?spm=a2c4e.11153940.blogcont65199.22.30f968210RsUSx&commodi ...

  10. 个人前端学习路线图与github优秀前端开发者的路线图推荐

    1.个人目前学习的路线图 2.github优秀前端开发者的路线图推荐 打开github首页,在搜索框输入developer-roadmap,搜索github前端路线图 选择kamranahmedse/ ...