<1> 字节流
<2> 字符流
<3> IO 流书写规范要求
这里有必要敲黑板强调: 所有的流操作,使用前,需要导入IO 包中的类,使用时,需进行IO 异常处理,使用后,需显式地关闭流来释放资源。
JDK 7 以后,只要实现AutoCloseable接口的类,都可以通过自动关闭资源的try 语句来关闭这些流。
<4> 输入输出流体系中常用流的分类
//创建一个文件输入流对象,并关联aaa.txt
FileInputStream fis = new FileInputStream( "aaa.txt" );
//定义变量,记录每次读到的字节
int b;
while((b=fis.read()) != -1){
//打印每一个字节
System.out.println(b);
}
FileWriter fr = new FileWriter( "bbb.txt" );
fr.write( 1 );
fr.write( "string" );
fr.write( new char[]{'a','b','c'} );
<1> FileInputStream读取,FileOutputStream写出,字节流一次读写一个字节复制音频(效率太低)
public class Main {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream( "XXX.mp3" ); //创建输入流对象,关联XXX.mp3
FileOutputStream fos = new FileOutputStream( "copy.mp3" );//创建输出流对象,关联copy.mp3
int b;
while ((b = fis.read()) != -1) {
fos.write( b );
}
fis.close(); //关闭流
fos.close(); //关闭流
}
}
**<2> 小数组读取(推荐)
import java.io.*;
import java.nio.file.Paths;
/**
* @Auther: SolarL
* @Date: 2019/4/3
* @Description: com.sunlong.file
* @version: 1.0
*/
public class Main {
public static void main(String[] args) throws IOException {
//异常处理(小数组拷贝)
FileInputStream fis = null;
FileOutputStream fos = null;
try {
File srcFile = Paths.get( "E:", "XXX.mp3" ).toFile();
File descFile = new File( "E:" + File.separator + "copy.mp3" );
fis = new FileInputStream( srcFile );
fos = new FileOutputStream( descFile );
byte[] arr = new byte[1024 * 8];
int len;
while ((len = fis.read( arr )) != -1) {
fos.write( arr, 0, len );
}
} finally {
try {
if (fis != null)
fis.close();
} finally { //try finally嵌套的目的是能关一个尽量关一个
if (fos != null)
fos.close();
}
}
}
}
<3>采用带缓冲的字节流读取(BufferedInputStream 和 BufferedOutputStream)
public class Main {
public static void main(String[] args) {
//实现AutoCloseable接口的类,都可以通过自动关闭资源的try语句来关闭这些流
try (
FileInputStream fis = new FileInputStream(
new File( "D:" + File.separator + "XXX.mp3" ) ); //创建文件输入流对象,关联XXX.mp3
BufferedInputStream bis = new BufferedInputStream( fis ); //创建缓冲区对fis装饰
FileOutputStream fos = new FileOutputStream( "D:" + File.separator +"copy.mp3" ); //创建输出流对象,关联copy.mp3
BufferedOutputStream bos = new BufferedOutputStream( fos ); //创建缓冲区对fos装饰 )
) {
int b;
while ((b = bis.read()) != -1) {
bos.write( b );
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.sunlong.java;
import java.io.*;
public class FileText3 {
//从键盘接收两个文件夹路径,把其中一个文件夹包含内容拷贝到另一个文件夹中
public static void main(String[] args) throws IOException {
//从键盘接收两个文件夹路径
File src = FileTest.getDir();
File dest = FileTest.getDir();
if (src.equals( dest )){
System.out.println("目标文件夹是原文件夹的子文件");
}else {
copyFileDir( src, dest );
}
}
//把其中一个文件夹包含内容拷贝到另一个文件夹中
/*
* 1.在目标文件夹中创建原文件夹
* 2.获取原文件夹中所有文件及文件夹,存储到File数组中
* 3.遍历数组
* 4.如果是文件就用IO流读写
* 5.如果是文件夹就递归调用
* */
public static void copyFileDir(File src, File dest) throws IOException {
// 1.在目标文件夹中创建原文件夹
File newFile = new File( dest, src.getName() );
newFile.mkdir();
File [] subFiles = src.listFiles();
for (File subFile : subFiles) {
if (subFile.isFile()){
BufferedInputStream bis = new BufferedInputStream( new FileInputStream( subFile ) );
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream( new File( newFile, subFile.getName() ) ) );
int b;
while ((b=bis.read()) != -1){
bos.write( b );
}
bis.close();
bos.close();
}else{
copyFileDir( subFile, newFile );
}
}
}
}
<1> BufferedReader 和 BufferedWriter
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader( new FileReader( "aaa.txt" ) );
BufferedWriter bw = new BufferedWriter( new FileWriter( "ddd.txt" ) );
String line;
while((line = br.readLine()) != null){
bw.write( line );
//bw.write("\r\n"); //只支持windows系统
bw.newLine(); //写出回车换行符,跨平台支持
}
br.close();
bw.close();
}
public static void demo04() throws IOException {
//1.创建带缓冲的输入流对象
BufferedReader br = new BufferedReader( new FileReader( "aaa.txt" ) );
//2.创建双列集合对象TreeMap
TreeMap<Character, Integer> tm = new TreeMap<>( );
//3.将读到的字符存储到双列集合,存储时要作判断,如果不包含这个键,将键和值1存储,包含则值加一
int ch;
while((ch= br.read()) != -1){
char c = (char) ch;
/* if(!tm.containsKey( c )){
tm.put( c,1 );
}else{
tm.put( c, tm.get( c ) + 1 );
}*/
tm.put( c,!tm.containsKey( c ) ? 1:tm.get( c ) +1 );
}
//4.关闭输入流
br.close();
//5.创建输出流对象
BufferedWriter bw = new BufferedWriter( new FileWriter( "Times.txt" ) );
// 6.遍历集合的内容写到times.txt中
for (Character key : tm.keySet()) {
switch (key){
case '\t':
bw.write( "\\t" + "="+ tm.get( key ) );
break;
case '\n':
bw.write( "\\n" + "="+ tm.get( key ) );
break;
case '\r':
bw.write( "\\r" + "="+ tm.get( key ) );
break;
default:
bw.write( key + "="+ tm.get( key ) );
break;
}
bw.newLine();
}
// 7.关闭输出流
bw.close();
}
<2> java.util.Scanner类
public static void main(String[] args) {
// 默认键盘输入
Scanner scanner = new Scanner( System.in );
//从指定文件text.txt 输入
try (Scanner scanner1 = new Scanner(
new FileInputStream(
new File( "E:" + File.separator + "text.txt" ) ) )) {
System.out.println( scanner1.nextLine() );
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
package com.sunlong.file;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.nio.file.Paths;
/**
* @Auther: SolarL
* @Date: 2019/4/3
* @Description: com.sunlong.file
* @version: 1.0
*/
public class Print {
public static void main(String[] args) {
try (
PrintStream ps = new PrintStream(
new FileOutputStream( Paths.get( "E:", "text.txt" ).toFile() ) );
) {
ps.printf( "%.2f", 1.2333 );//格式化输出 1.23
ps.print( "aaa" ); //传入字符串
ps.print( true ); //传入布尔值
ps.print( 1110 ); //传入int数
ps.print( 'a' ); //传入字符
ps.print( new char[2] ); //传入字符数组
ps.print( new Person()); //传入自定义对象
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
class Person{
}
上篇及本篇就是Java I/O 的输入/输出体系相关知识,如何使用 File 类来访问本地文件系统,Java 不同IO 流的功能,以及几种典型流的用法,其余流在此不再介绍。
[Java I/O 流] 带你一起玩转输入/输出流(上)跳转链接:
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- axer.cn 版权所有 湘ICP备2023022495号-12
违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务