java.io.File:文件和目录(文件夹)路径名的抽象表示形式。
* 构造方法
*
* File(String parent, String child)
* File(String pathname)
* * 通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。
* File(File parent, String child)
* 根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。
public class FileDemo {
public static void main(String[] args) {
//请使用File文件类:描述D盘下的aaa文件夹下的a.txt文件
//D:\aaa\a.txt
// File(String parent, String child):参数1:父目录 参数2:指定文件或者文件夹
File file1 = new File("D:\\aaa","a.txt") ;
System.out.println(file1);//D:\aaa\a.txt
System.out.println("-----------------------------");
//File(String pathname):直接指定具体的文件路径 (推荐第二种)
File file2 = new File("d:\\aaa\\a.txt") ;
System.out.println(file2); // d:\aaa\a.txt
System.out.println("-----------------------------");
// File(File parent, String child)
File file3 = new File("d:\\aaa") ;
File file4= new File(file3,"a.txt") ;
System.out.println(file4);//d:\aaa\a.txt
}
}
File类的相关的文件夹的创建
* 文件的创建
* 文件夹以及文件的删除功能
*
* public boolean mkdir() :创建文件夹:如果已经存在,创建不成功,返回false!
* public boolean mkdirs():创建多级目录的使用,如果父文件夹不存在,自动创建
*
* public boolean createNewFile()throws IOException:创建文件,可能出现IO异常(举例:文件找不到)
*
* public boolean delete():删除文件或者是文件夹
public class FileDemo2 {
public static void main(String[] args) throws IOException {
//描述目录:
//D:\JavaEE_2104\EE_day24创建一个code目录
// File file = new File("D:\\JavaEE_2104\\EE_day24\\code") ;
// public boolean mkdir() :创建文件夹:如果已经存在,创建不成功,返回false!
//System.out.println(file.mkdir());
//public boolean mkdirs():创建多级目录的使用,如果父文件夹不存在,自动创建
//D:\JavaEE_2104\EE_day24\code 创建很多个目录
//File file2 = new File("D:\\JavaEE_2104\\EE_day24\\code\\aaa\\bbb\\ccc") ;
//System.out.println(file2.mkdirs());
//public boolean createNewFile()throws IOException:创建文件,可能出现IO异常(举例:文件找不到)
//D:\JavaEE_2104\EE_day24\code 里面创建a.txt文件
/*File file3 = new File("D:\\JavaEE_2104\\EE_day24\\code\\a.txt") ;
System.out.println(file3.createNewFile());*/
//public boolean delete():删除文件或者是文件夹
//D:\JavaEE_2104\EE_day24\code :删除
// File file4 = new File("D:\\JavaEE_2104\\EE_day24\\code") ;
//System.out.println(file4.delete());
//先删除文件,在删除目录
/* File file4 = new File("D:\\JavaEE_2104\\EE_day24\\code\\a.txt") ;
System.out.println(file4.delete());
File file5 = new File("D:\\JavaEE_2104\\EE_day24\\code") ;
System.out.println(file5.delete());*/
//上面的路径:都是带盘符
//创建一个demo文件夹 如果没有指定盘符:相对路径:默认在当前项目路径的下创建文件或者是文件
File file = new File("demo") ;
System.out.println(file.mkdir());
File file2 = new File("demo\\a.txt") ;
System.out.println(file2.createNewFile());
}
}
重命名功能:
* public boolean renameTo(File dest):对当前文件进行重命名
*
* 情况1:
* 当前指定文件路径和重命名后的路径一致的,那么仅仅是改名字
*
* 需要将D盘下的高圆圆.jpg改名个为杨桃.jpg
* D:\\高圆圆.jpg
*
* D:\\杨桃.jpg
*
*
* 情况2:如果路径不一致, ----改名并剪切
*
* 需要将当前项目下的杨桃.jpg
*
*
* D:\\高圆圆.jpg
File file = new File("d:\\高圆圆.jpg") ;
File file2 = new File("d:\\杨桃.jpg") ;
// public boolean renameTo(File dest):对当前文件进行重命名
//File file = new File("杨桃.jpg") ;
//File file2 = new File("d:\\高圆圆.jpg") ;
System.out.println(file.renameTo(file2));
File类的高级获取功能:
* public String[] list():当前目录下的所有的文件夹以及文件的字符串数组
* public File[] listFiles():获取当前某个目录下的所有的File数组对象
*
* 需求:
* 获取d盘下的所有的以.jpg结尾的文件
*
* 1)获取当前某个盘符下或者目录下的所有的文件以及文件夹的file数组
* public File[] listFiles():获取当前某个目录下的所有的File数组对象
*
* 2)防止空指针:判断如果当前File数组不为空的时候,遍历
* 遍历,获取到每一个File对象
* 2.1)File对象如果是文件 isFile()
* 2.2)并且它还是以.jpg结尾的文件
* 2.3)输出文件对象.getName() 获取到
public class FileDemo5 {
public static void main(String[] args) {
//获取D盘下的所有文件以及文件数组
File file = new File("d:\\") ;
/* String[] strArray = file.list();
for(String s: strArray){
System.out.println(s);
}*/
File[] fileArray = file.listFiles();
if(fileArray!=null){
for(File f :fileArray){
//获取名称
//public String getName():获取文件或者目录的名称
// System.out.println(f.getName());
if(f.isFile()){
//再次判断
if(f.getName().endsWith(".jpg")){
System.out.println(f.getName());
}
}
}
}
}
}
判断功能
* public boolean canRead():判断这个文件是否是可读
* public boolean canWrite():是否可写
* public boolean exists():是否存在
* public boolean isDirectory():是否是文件夹 (使用居多)
* public boolean isFile():是否是文件 (使用居多)
* public boolean isHidden():是否是隐藏文件
获取d盘下的所有的以.jpg结尾的文件
*
* File类获取功能:
* 使用文件名称过滤器来帮助我们完成过滤!
*
* 优点:在获取的时候就已经通过文件名称过滤器筛选了
*
* 获取字符串列表
* public String[] list(FilenameFilter filter)
* 获取文件列表
* public File[] listFiles(FilenameFilter filter) 形式参数接口:需要接口子实现类对象
* 使用匿名内部类的方式
*
* FilenameFilter:文件名称过滤器
* boolean accept(File dir,String name)
* 返回值true或者false取决于:是否将当前name文件添加在文件列表中
public class FileTest {
public static void main(String[] args) {
//描述D盘
File file = new File("d:\\") ;
// public File[] listFiles(FilenameFilter filter) 形式参数接口:需要接口子实现类对象
File[] fileArray = file.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
// return false;
//封装name---File对象
/* File file = new File(dir,name) ;
//分步走
boolean flag1 = file.isFile();
boolean flag2 = file.getName().endsWith(".jpg");
return flag1 && flag2 ;*/
File file = new File(dir,name);
//一步走
return file.isFile() && file.getName().endsWith(".jpg") ;
}
});
for(File f :fileArray){
System.out.println(f.getName());
}
}
}
1.列出File类的一些常见功能
File常见的功能:
判断功能:
isFile():是否是文件
isDirectory():是否是文件夹
获取功能:
String[] list():获取某个目录下的所有的文件夹以及文件的名称字符串数组
File[] listFiles():某个目录下的所有的文件以及文件夹的File数组
带参:接口 文件名称过滤器
File[] listFiles(FilenameFilter accpet):
获取的时候就已经将满足条件的文件或者文件进行过滤
2.多线程创建的方式
三种:
1)继承自Thread类
2)静态代理:实现Runnable接口
3)线程池:ExecutorService (接口)
ThreadPoolExecutor(子实现类)
3.获取字节码文件的方式有几种
三种:
1)Object的getClass()方法
2)任意Java类型的class属性
3)反射:Class.forName("类的全限定名称:包名.类名") ; (推荐)
4.同步方法的锁对象和静态同步的锁对象?
同步方法锁对象--->当前类对象的地址值引用:this
静态同步方法锁对象--->当前类名.class(当前类的字节码文件对象 class 包名.类名)
5.线程的状态有几种
六种
Thread 枚举State
NEW 新建
RUNNABLE 执行
BLOCKED 阻塞
WAITTING 等待(死死等待: join(),wait()---通过唤醒来结束等待)
TIMED_WAITTING 超时等待(wait(long time) ,join(long time)..)
TERMINATED 终止状态
IO流的分类
*
* 按流的方向划分:
* 1)输入流
* 2)输出流
*
* 按流的类型划分:
* 1)字节流
* 字节输入流:InputStream(抽象类)
* FileInputStream:针对文件操作的字节输入流(读)
*
* 字节缓冲输入流(字节高效输入流):BufferedInpuStream
* 字节输出流:OutputStream(抽象类)
* FileOutputStream:针对文件操作的字节输出流(写)
*
*
* 字节缓冲输出流(字节高效输出流):BufferedOutputStream
*
*
* 2)字符流:由于字节流操作文本文件的时候(一次读取一个字节的时候,将内容输出在控制台上:可能出现中文乱码)
* 所以才有了字符流!
*
* 字符输入流:Reader:读
* 字节输入流通向字符输入流的桥梁(转换输入流):InputStreamReader
* 为了书写简单:FileReader
*
*
* 字符缓冲输入流(字符高效输入流):BufferedReader
* 特有功能:String readLine() :一次读取一行
*
*
* 字符输出流:Writer:写
* 字节输出流流通向字符输出流的桥梁(转换输出流):OutputStreamWriter
* 为了书写简单:FileWriter
*
*
* 字符缓冲输出流(字符高效输出流):BufferedWriter
* 特有功能:
* public void newLine():写入一个行的分隔(换行功能)
*
*
*字节流:
* 字节输出流:OutputStreamStream抽象类
* 提供具体的子类:FileOutputStream
*
*
* 使用步骤 :
* 1)创建一个文件输出流对象
* 2)写一些内容(输出到某个盘符下或者当前项目下)
* 3)释放资源:流资源(流的底层----->非Java语言实现:本地方法)
*
* 文件----->都是跟系统相关的(主机环境)
*
* 构造方法:
* FileOutputStream(File file)
* File file = new File("a.txt") ;
* new FileOutputStream(file) ;
*
* public FileOutputStream(String name) throws FileNotFoundException(推荐:因为参数可以直接指定一个路径)
*
public class FileOutputStreamDemo {
public static void main(String[] args) throws IOException {
//创建一个文件输出流对象
//抽象类多态
// OutputStream out = new FileOutputStream("fos.txt") ;
//具体类创建
FileOutputStream fos = new FileOutputStream("fos.txt") ; //调用系统资源 执行当前地址:文件fos.txt
//写
fos.write("hello,io".getBytes());
//释放资源:将当前系统资源指向的这个文件将它从内存中释放调用
fos.close() ;
}
}
字节输出流:写的功能
*
* public void write(int b) throws IOException:给当前字节输出流中写入一个字节
* public void write(byte[] b) throws IOException:写入一个字节数组
* public void write(byte[] b,int off,int len)throws IOException:写一个字节数组的一部分
* 参数2:从指定位置开始
* 参数3:指定长度
public class FileOutputStreamDemo2 {
public static void main(String[] args) throws IOException {
//创建一个输出流对象
FileOutputStream fos = new FileOutputStream("fos2.txt") ;
//public void write(int b) throws IOException:给当前字节输出流中写入一个字节
/* fos.write(97); //打开文件---这个字节就寻找对应的ASCII码表的值!
fos.write(65);
fos.write(48);*/
// public void write(byte[] b) throws IOException:写入一个字节数组
byte[] bytes = {97,98,99,101} ;
fos.write(bytes) ;
//public void write(byte[] b,int off,int len)throws IOException:写一个字节数组的一部分
fos.write(bytes,0,2);
//释放资源
fos.close() ;
}
}
* public FileOutputStream(String name,boolean append) throws FileNotFoundException
* 创建一个文件输出流对象,第二个参数为true,就是在文件字节末尾处追加!
*
* 如何现在将输出一句内容,换一行?
*
* windows操作系统
* 换行符号"\r\n"
public class FileOutputStreamDemo3 {
public static void main(String[] args) throws IOException {
//创建一个文件输出流对象,开启文件末尾字节追加
FileOutputStream fos = new FileOutputStream("fos3.txt",true) ;
//for循环
for(int x = 0 ; x < 10 ; x ++){
fos.write(("hello"+x).getBytes());
//写入一个换行符号
fos.write("\r\n".getBytes());
}
//关闭资源
fos.close();
}
}
流中加入异常操作:
*
* 解决方案---->try...catch...finally 标准格式
public class FileOutputStreamDemo4 {
public static void main(String[] args) {
//method1() ;
// method2() ;
method3() ;
}
//第三种方式:try...catch...finally
private static void method3() {
FileOutputStream fos = null ;
try {
fos = new FileOutputStream("fos4.txt") ;
fos.write("hello,i'm coming...".getBytes());
} catch (IOException e) {
e.printStackTrace(); //如果try有问题:代码追踪到源码中,并且有jvm将异常信息以及源码的错误行数都可以 控制台日志体现
}finally {
if(fos!=null){
//释放资源
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//第二种方式:try...catch...catch
private static void method2() {
//创建一个文件输出流对象
FileOutputStream fos = null;
try {
fos = new FileOutputStream("fos4.txt");
//写数据
fos.write("hello,JavaEE".getBytes());
//关闭资源
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//第一种方式:就是分别try...catch
private static void method1() {
//创建一个文件输出流对象
FileOutputStream fos = null ;
try {
fos = new FileOutputStream("fos4.txt") ;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//写数据
try {
fos.write("hello,OutputStream".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
//释放资源
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
抽象类:InputStream:表示字节输入流的所有类的超类
* 提供的针对文件操作的输入流:FileInputStream
*
* 使用步骤:
* 1)创建文件操作输入流对象
* public InputStream()
*
* 2)通过 文件字节输入流对象,读取指定的文件中的内容
* 读取数据的功能:
* public int read() throws IOException:一次读取一个字节
* public int read(byte[] b) throws IOException: 读取一个字节数组,返回的是读取的实际字节数
* public int read(byte[] b,int off,int len) throws IOException:读取一部分字节数组
*
* 3)释放资源
*
* 一次读取一个字节:
* 读取一些文件,中文会出现乱码,原因就是将实际的字节数强制转为char类型,
* 而中文在idea平台:utf-8格式:一个中文对应三个字节,和前面的英文拼接不到一起,就出现中文乱码
*
public class FileInputStreamDemo {
public static void main(String[] args) throws IOException {
//创建文件操作输入流对象
// FileInputStream fis = new FileInputStream("fis.txt") ;
//读取当前项目下的FileOutputStreamDemo
FileInputStream fis = new FileInputStream("FileOutputStreamDemo.java") ;
//读
// public int read() throws IOException:一次读取一个字节
/* //第一次读
int by = fis.read();
System.out.println((char)by);
//第二次读
by = fis.read() ;
System.out.println((char)by);
//第三次读
by = fis.read() ;
System.out.println((char)by);
//第四次读
by = fis.read() ;
System.out.println((char)by);
//第五次读
by =fis.read() ;
System.out.println(by);
System.out.println((char)by);*/
//结束条件就是看返回结果为-1
//将上面的结果进行优化
/* int by = fis.read() ;//一次读取一个字节
while(by!=-1){
System.out.print((char)by);
by = fis.read() ;
}*/
//最终版代码
//将获取,判断,赋值直接一次性使用
//声明一个字节数
int by = 0 ;
while((by=fis.read())!=-1){
System.out.print((char)by);
}
//释放资源
fis.close();
}
}
public int read(byte[] b) throws IOException: 读取一个字节数组,返回的是读取的实际字节数
*
*
* 字节输入流 一次读取一个字节/一次读取一个字节数组 (第二种方式的读写速率优于第一种方式!)
//返回结果是否为-1:作为读取完毕的结束条件
//优化 :byte[] bytes = new byte[5] ;定义的字节数组:缓冲区长度得多大?
//一般是1024(足够大)或者1024的整数倍
//最终版代码
byte[] bufferBytes = new byte[1024] ;
//实际字节数
int len = 0 ;
//赋值,判断,获取
while((len=fis.read(bufferBytes))!=-1){
//获取内容 :一定带上 0,实际字节数
System.out.println(new String(bufferBytes,0,len));
将当前项目下的高圆圆.jpg复制到
* D:\JavaEE_2104\EE_day25\code\mv.jpg
private static void copy2(String srcFile, String destFile) throws Exception {
//封装源文件--字节输入流对象
fis = new FileInputStream(srcFile) ;
//封装目的地文件
fos = new FileOutputStream(destFile) ;
//读写操作
byte[] bytes = new byte[1024] ;
int len = 0 ;
while((len=fis.read(bytes))!=-1){
fos.write(bytes,0,len);
}
close(fos,fis);
}
将当前项目下的FileOutputStreamDemo.java 复制到D:\JavaEE_2104\EE_day25\code Copy.java
*
* 分析:
* 源文件---->当前项目下的FileOutputStreamDemo.java
* 使用文件字节输入流:FileInputStream :一次读取一个字节/一次读取一个字节数组
* 当前输入流对象指向:FileOutputStreamDemo.java
*
* 目标文件--->D:\JavaEE_2104\EE_day25\code\Copy.java
* 使用文件字节输出流:FileOutputStream: 写一个字节/写一个字
FileInputStream fis = new FileInputStream("FileOutputStreamDemo.java") ;
//创建一个文件字节输出流对象
FileOutputStream fos = new FileOutputStream("D:\\JavaEE_2104\\EE_day25\\code\\Copy.java") ;
long start = System.currentTimeMillis() ;
//读写复制操作
//方式1:一次读取一个字节
/* int by = 0 ;
while((by=fis.read())!=-1){
//读一个字节,写入一个字节
fos.write(by);
}*/
//方式2:一次读取一个字节数组
byte[] bytes = new byte[1024] ;
int len = 0 ;
while((len=fis.read(bytes))!=-1){
//写入一个字节数组,并且从0开始,写入实际的字节数
fos.write(bytes,0,len);
}
long end = System.currentTimeMillis() ;
System.out.println("共耗时:"+(end-start)+"毫秒");
//关闭资源
fos.close();
fis.close();
}
}
针对一个文件读写复制几种方式?
* 基本的字节流:一次一个字节
* 基本的字节流:一次一个字节数组
*
* 字节缓冲流:一次一个字节
* 字节缓冲流:一次一个字节数组
Java提供了比一次读取一个字节数组更高效的方式:缓冲流
* BufferedOutputStream
* BufferedOutputStream(OutputStream out) :指定默认的缓冲长度:足够大了
* 写
* public void write(int b)
* public void write(byte[] b,int off,int len)
* BufferedInputStream
* BufferedInputStream(InputStream in)
* 读
* public int read()
* public int read(byte[] bytes)
* public int read(byte[] bytes,int offset,int len)
*
*
* 内部只是提供一个字节数组(缓冲区):长度8192个长度
* 构造方法都是默认的缓冲区大小,文件的操作还需要通过最底层的基本的字节流操作
* 缓冲流都是不直接操作文件,仅仅是提供内部缓冲区(提高读写效率!)
*
*
* //错误的: BufferedOutputStream bos = new BufferedOutputStream("bos.txt") ;
public class BufferedStreamDemo {
public static void main(String[] args) throws Exception{
//创建一个字节缓冲输出流对象
//BufferedOutputStream(OutputStream out)
//内部提供的技术一个字节数组:byte[] buf = new byte[size]; //size:8192(默认缓冲区长度)
/* BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream("bos.txt")) ;
bos.write("hello,BufferedOutputStream".getBytes());
//关闭流
bos.close();*/
//创建一个字节缓冲输入流对象:
// BufferedInputStream(InputStream in)
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream("FileOutputStreamDemo.java")) ;
//类名(抽象类 参数)---->装饰者设计模式
//1)当前类名
//2)形式参数:一般为当期类的父类(抽象的)
//3)还要提供抽象类的具体的子类
//一次读取一个字节
/* int by = 0 ;
while((by = bis.read())!=-1){
System.out.print((char)by);
}*/
//一次读取一个字节数组
byte[] bytes = new byte[1024] ;
int len = 0 ;
while((len=bis.read(bytes))!=-1){
System.out.println(new String(bytes,0,len));
}
//关闭
bis.close();
}
}
字符输入
* Reader(抽象类)
* InputStreamReader 是字节流通向字符流的桥梁
* 构造方法:
* public InputStreamReader(InputStream in):使用默认字符集进行解码(utf-8:跟idea的配置有关系)
* public InputStreamReader(InputStream in,String charsetName):使用指定的字符集进行解码
*
*
* 在读写的时候,必须要保证编码和解码统一!
public static void main(String[] args) throws Exception{
//创建字符输入流对象:使用字符转换输入流
/* InputStreamReader isr =
new InputStreamReader(new FileInputStream("osw.txt"),"gbk") ;*/
InputStreamReader isr =
new InputStreamReader(new FileInputStream("osw.txt"));
/* int by = isr.read();
System.out.println(by);*/
//一次读取一个字符
/* int by = 0 ; //字符数
while((by=isr.read())!=-1){
System.out.print((char)by); //字符数 (单个字符会ASCII码表的值)
}*/
//一次读取一个字符数组
char[] chs = new char[1024] ;
int len = 0 ;
while((len=isr.read(chs))!=-1){
System.out.println(new String(chs,0,len));
}
//释放
isr.close();
}
}
键盘录入三种
* 1)main方法的参数(早期的)
* 2)jdk5以后提供的Scanner
* 3)BufferedReader(Reader r)
public class Test {
public static void main(String[] args) throws Exception{
//创建一个字符缓冲输入流对象
// Scanner sc = new Scanner(System.in) ;
InputStream in = System.in ;
//抽象类指向子类
Reader r = new InputStreamReader(in) ;
BufferedReader br = new BufferedReader(r) ;
//提示
System.out.println("请您输入一个int类型的内容:");
String line = br.readLine(); //阻塞式方法: 不录入内容,一直等待
int i = Integer.parseInt(line); //line:数字字符串
System.out.println("您输入的数据是:"+i);
}
}
使用字符转换流将当前项目下的:CopyMp4.java复制到D:\EE_2104\EE_day25\code a.java
*
* D:\JavaEE_2104\EE_day25\code\a.java
*
*
* 读写复制:
* 针对文本文件:.txt/java---- 都是直接使用字符流!
*
* 针对图片文件,视频文件,音频文件等等----都是直接使用字节流!
*
*
*
* 字符转换流的便捷类:只是为了书写简单
* InputStreamReader---->FileReader(File file)/FileReader(String pathname) :平台默认的解码集(idea---utf-8)
* OutputStreamWriter --->FileWriter(File file)/FileWriter(String pathname):默认的编码
public class CopyFile {
public static void main(String[] args) throws Exception{
//创建转换输入流对象 解码 平台默认编码集
/*InputStreamReader isr =
new InputStreamReader(new FileInputStream("CopyMp4.java")) ;
//字符转换输出流 :编码 平台默认编码集
OutputStreamWriter osw =
new OutputStreamWriter(new FileOutputStream("D:\\JavaEE_2104\\EE_day25\\code\\a.java"));*/
//针对文件操作的字符输入流
FileReader fr = new FileReader("CopyMp4.java") ;
//针对文件操作的字符输出流
FileWriter fw = new FileWriter("D:\\JavaEE_2104\\EE_day25\\code\\a.java");
//一次读取一个字符数组
char[] chs = new char[1024] ;
int len = 0 ;
while((len=fr.read(chs))!=-1){
//写
fw.write(chs,0,len);
//刷新
fw.flush() ;
}
//关闭
fw.close();
fr.close();
}
}
字符缓冲输入流和字符缓冲输出流:提供默认的缓冲区大小,读写的操作还需要交给底层字节流
* 为了针对文本文件操作,提高执行效率!
* BufferedWriter
* 将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。
* 构造方法
* BufferedWriter(Writer out)
* 成员方法:
* public void newLine():换行功能
*
*
*
* 字符缓冲输入流:BufferedReader
* 构造方法:
* public BufferedReader(Reader in)
*
* 特有功能:
* public String readLine()
字符流
* 字符输入
* 字符输出
*
* Writer(抽象类)
*
* 子类:字符转换输出流:OutputStreamWriter 是字符流通向字节流的桥梁
*
* 构造方法:
* OutputStreamWriter(OutputStream out) :创建转换输出流对象:平台默认编码集(utf-8)
* OutputStreamWriter(OutputStream out,String charsetName):使用指定的字符集编码
OutputStreamWriter osw = new OutputStreamWriter(
new FileOutputStream("osw.txt")) ;
OutputStreamWriter osw = new OutputStreamWriter(
new FileOutputStream("osw.txt")) ;
osw.write("hello,字符流");
//关闭流的之前,刷新该流(将字符流的内存中底层保存的字节刷新)
osw.flush();
//关闭资源 --- 关闭系统资源指向文件的 线程!
osw.close(); //内存对象已经为null(流已经关闭)
1万+

被折叠的 条评论
为什么被折叠?



