初识io
代码演示
private final static String URL = "E:\\";
private final static String READURL = "H:\\kj\\202402\\LS0205.00P";
@Test
public void testOutputStream() {
long start = System.currentTimeMillis();
try (
InputStream in = new FileInputStream( READURL );
OutputStream out = new FileOutputStream( URL + "FileOutputStream输出流.txt" );
) {
int len;
while ((len = in.read()) != -1) {
out.write( (char) len );
}
} catch (Exception e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println( "消耗时间" + (end - start) / 1000.0 + "秒" );
}
@Test
public void testOutputStreamSz() {
long start = System.currentTimeMillis();
try (
InputStream in = new FileInputStream( READURL );
OutputStream out = new FileOutputStream( URL + "FileOutputStream输出流字节数组.txt" );
) {
byte[] bytes = new byte[1024 * 80];
int lent;
while ((lent = in.read( bytes )) != -1) {
out.write( bytes, 0, lent );
}
} catch (Exception e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
Runtime runtime = Runtime.getRuntime();
System.out.println( "最大可用内存: " + runtime.maxMemory() / 1024 );
System.out.println( "已分配内存: " + runtime.totalMemory() / 1024 );
System.out.println( "可用内存: " + runtime.freeMemory() / 1024 );
System.out.println( "已使用内存: " + (runtime.totalMemory() - runtime.freeMemory()) / 1024 );
System.out.println( "消耗时间" + (end - start) / 1000.0 + "秒" );
/* 最大可用内存: 253440
已分配内存: 15872
可用内存: 11551
已使用内存: 4320
消耗时间1.343秒*/
}
@Test
public void testBufferedSz() {
long start = System.currentTimeMillis();
try (
InputStream inputStream = new FileInputStream( READURL );
BufferedInputStream in = new BufferedInputStream( inputStream, 1024 * 80 );
OutputStream outputStream = new FileOutputStream( URL + "Buffered输出流字节数组.txt" );
OutputStream out = new BufferedOutputStream( outputStream, 1024 * 80 );
) {
byte[] bytes = new byte[1024 * 80];
int lent;
while ((lent = in.read( bytes )) != -1) {
out.write( bytes, 0, lent );
}
} catch (Exception e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
Runtime runtime = Runtime.getRuntime();
System.out.println( "最大可用内存: " + runtime.maxMemory() / 1024 );
System.out.println( "已分配内存: " + runtime.totalMemory() / 1024 );
System.out.println( "可用内存: " + runtime.freeMemory() / 1024 );
System.out.println( "已使用内存: " + (runtime.totalMemory() - runtime.freeMemory()) / 1024 );
System.out.println( "消耗时间" + (end - start) / 1000.0 + "秒" );
/* 最大可用内存: 253440
已分配内存: 15872
可用内存: 11572
已使用内存: 4299
消耗时间1.156秒* */
}
@Test
public void testFileReaderSz() {
long start = System.currentTimeMillis();
try (
Reader in = new FileReader( READURL );
Writer out = new FileWriter( URL + "FileReaderSz输出流字符数组.txt" );
) {
char[] chars = new char[1024 * 80];
int lent;
while ((lent = in.read( chars )) != -1) {
out.write( chars, 0, lent );
}
} catch (Exception e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
Runtime runtime = Runtime.getRuntime();
System.out.println( "最大可用内存: " + runtime.maxMemory() / 1024 );
System.out.println( "已分配内存: " + runtime.totalMemory() / 1024 );
System.out.println( "可用内存: " + runtime.freeMemory() / 1024 );
System.out.println( "已使用内存: " + (runtime.totalMemory() - runtime.freeMemory()) / 1024 );
System.out.println( "消耗时间" + (end - start) / 1000.0 + "秒" );
/* 最大可用内存: 253440
已分配内存: 15872
可用内存: 11407
已使用内存: 4464
消耗时间5.171秒 */
}
@Test
public void testBufferedReaderSz() {
long start = System.currentTimeMillis();
try (
Reader reader = new FileReader( READURL );
BufferedReader in = new BufferedReader( reader, 1024 * 80 );
Writer writer = new FileWriter( URL + "BufferedReader输出流字符数组.txt" );
BufferedWriter out = new BufferedWriter( writer, 1024 * 80 );
) {
char[] chars = new char[1024 * 80];
int lent;
while ((lent = in.read( chars )) != -1) {
out.write( chars, 0, lent );
}
} catch (Exception e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
Runtime runtime = Runtime.getRuntime();
System.out.println( "最大可用内存: " + runtime.maxMemory() / 1024 );
System.out.println( "已分配内存: " + runtime.totalMemory() / 1024 );
System.out.println( "可用内存: " + runtime.freeMemory() / 1024 );
System.out.println( "已使用内存: " + (runtime.totalMemory() - runtime.freeMemory()) / 1024 );
System.out.println( "消耗时间" + (end - start) / 1000.0 + "秒" );
/* 最大可用内存: 253440
已分配内存: 15872
可用内存: 11376
已使用内存: 4495
消耗时间5.015秒 * */
}
@Test
public static void testWrit() {
try (
BufferedReader reader = new BufferedReader( new FileReader( "src/test/java/tjqs/hy/file.txt" ) );
BufferedWriter writer = new BufferedWriter( new FileWriter( "src/test/java/tjqs/hy/file1.txt", true ) ); //有TRUE不会覆盖以前的数据,没有或者是FALSE就会覆盖原来的数据 不使用多态的原因是要使用BufferedInputStream这个安行读取的方法,如果不使用readLin方法就可以使用多台
) {
String len;
while ((len = reader.readLine()) != null) {
writer.write( len, 0, len.length() );
writer.write( "\n" );
}
System.out.println( "=======================" );
} catch (Exception e) {
e.printStackTrace();
}
}
//字节转换流,当文件编码跟代码编码不一致是
@Test
public static void testZj() {
try (
//原始文件
InputStream in = new FileInputStream( "src/test/java/tjqs/hy/file_gbk.txt" );
//目标文件
BufferedWriter out = new BufferedWriter( new FileWriter( "src/test/java/tjqs/hy/file_gbk1.txt" ) );
//把原始的字节输入流按照指定的字符集编码转换成字符流编码
Reader is = new InputStreamReader( in, "GBK" ); //in:是流通到,"GBK":是原始文件的文件编码 对应的代码是utf-8
//把字符集输入流包装成缓冲字符输入流
BufferedReader bufferedReader = new BufferedReader( is );
) {
String len;
while ((len = bufferedReader.readLine()) != null) {
out.write( len, 0, len.length() );
out.write( "\n" );
}
} catch (Exception e) {
e.printStackTrace();
}
}