JavaIO简单代码实例
最近又复习了下JavaIO写了些实例代码都很简单但是能体现大部分方法的用法。
IO流实现文件的拷贝 几种不同的方法:
package com.wxisme.TestIO; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; /**
* 字节流拷贝文本文件
* @author wxisme
*
*/ public class StreamOne { public static void main(String[] args) {
String path = "E:" + File.separator + "test.txt";
File file = new File(path);
try {
file.createNewFile();
} catch (IOException e) {
System.out.println("创建文件失败");
e.printStackTrace();
}
InputStream is = null;
try {
is = new FileInputStream(file);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
String fileName = "E:" + File.separator + "Bullet.java";
OutputStream os = null;
try {
os = new FileOutputStream(fileName,true);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
byte[] b = new byte[10];
int len = 0;
try {
while((len = is.read(b)) != -1) {
os.write(b, 0, len);
}
os.flush();//强制刷出缓冲区
} catch (IOException e) { e.printStackTrace();
}finally {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
} System.exit(0); } }
package com.wxisme.TestIO; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer; /**
* 字节流拷贝文件
* @author wxisme
*
*/ public class StreamTwo { public static void main(String[] args) {
String path = "E:" + File.separator + "test.txt";
String name = "E:" + File.separator + "Bullet.java";
Reader r = null;
try {
r = new FileReader(name);
} catch (FileNotFoundException e) {
System.out.println("创建字符输入流失败");
e.printStackTrace();
}
Writer w = null;
try {
w = new FileWriter(path);
} catch (IOException e) {
System.out.println("创建字符输出流失败");
e.printStackTrace();
}
char[] cbuf = new char[10];
int len = 0;
try {
while((len = r.read(cbuf)) != -1) {
//w.write(cbuf);
w.write(cbuf, 0, len);
}
w.flush();//强制刷出
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
w.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
r.close();
} catch (IOException e) {
e.printStackTrace();
}
} } }
package com.wxisme.TestIO; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Scanner; /**
* 重定向输入输出 实现文件的拷贝
* @author wxisme
*
*/ public class Reset { public static void main(String[] args) throws FileNotFoundException {
String src = "E:" + File.separator + "Bullet.java";
String path = "E:" + File.separator + "test.txt";
FileInputStream fis = new FileInputStream(src);
System.setIn(fis); PrintStream ps = new PrintStream(new FileOutputStream(path));
System.setOut(ps); Scanner scan = new Scanner(System.in);
scan.useDelimiter("\n");//以换行符为分隔符
while(scan.hasNext()) {
System.out.println(scan.next());
}
} }
处理流PrintStrream PrintWriter
package com.wxisme.TestIO; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter; /**
* 使用处理流printStream printWriter
* @author wxisme
*
*/ public class StreamFour { public static void main(String[] args) throws IOException {
String path = "E:" + File.separator + "test.txt";
PrintStream ps = new PrintStream(new FileOutputStream(path,true));
ps.print("PrintStream");
PrintWriter pw = new PrintWriter(new FileWriter(path));
pw.print("PrintWriter");
pw.close();
ps.close();
} }
读取单个字符
package com.wxisme.TestIO; import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.Scanner; /**
* 读取单个字符
* @author wxisme
*
*/ public class ReaderOne { public static void main(String[] args) throws IOException {
Scanner scan = new Scanner(System.in);
String str = scan.next();
Reader r = new StringReader(str);
char[] cbuf = new char[10];
while(r.read(cbuf,0,1) != -1) {
char c = cbuf[0];
System.out.println(c);
}
r.close(); } }
转换流
package com.wxisme.TestIO; import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException; /**
* 使用转换流
* @author wxisme
*
*/ public class StreamThree { public static void main(String[] args) throws IOException { /*
* 输入流底层使用字节流,然后使用转换流把字节流转换成字符流,并且指定解码字符集。
* 然后把字符流包装成缓冲流,按行读取文件。
* 乱码问题的两个主要原因:
* 1. 解码字符集与编码字符集不统一
* 2. 读取字节缺少,长度丢失。
*/
String path = "E:" + File.separator + "Bullet.java";
BufferedReader br = new BufferedReader(
new InputStreamReader(
new FileInputStream(
new File(path)),"utf-8"));
String str;
while((str = br.readLine()) != null) {
System.out.println(str);
}
br.close(); } }
关闭文件的工具方法的两种写法:
package com.wxisme.TestIO; import java.io.Closeable;
import java.io.IOException; /**
* 关闭文件资源的工具类 有两种实现方法 可以实现一次关闭多个文件,先打开的文件后关闭
* @author wxisme
*
*/ public class CloseAll { /**
* 使用多态 可变参数 可以有多个形参以数组的形式 可变形参必须在所有参数的最后
* @param io
*/
public static void closeAll1(Closeable ... io) {
for(Closeable temp : io) {
if(temp != null) {
try {
temp.close();
} catch (IOException e) {
System.out.println("文件关闭失败");
e.printStackTrace();
}
}
}
} /**
* 泛型方法 使用泛型方法和可变参数
* @param io
*/ public static <T extends Closeable> void closeAll2(Closeable ... io) {
for(Closeable temp : io) {
if(temp != null) {
try {
temp.close();
} catch (IOException e) {
System.out.println("文件关闭失败");
e.printStackTrace();
}
}
}
} }
JavaIO简单代码实例的更多相关文章
- Redis:安装、配置、操作和简单代码实例(C语言Client端)
Redis:安装.配置.操作和简单代码实例(C语言Client端) - hj19870806的专栏 - 博客频道 - CSDN.NET Redis:安装.配置.操作和简单代码实例(C语言Client端 ...
- 从一些简单代码实例彻底理解面向对象编程思想|OOP本质是什么?
从Rob Pike 的 Google+上的一个推看到了一篇叫<Understanding Object Oriented Programming>的文章,我先把这篇文章简述一下,然后再说说 ...
- input文本框实现宽度自适应代码实例
代码实例如下: <!DOCTYPE html> <html><head><meta charset="utf-8"><meta ...
- jQuery实现的鼠标滑过切换图片代码实例
jQuery实现的鼠标滑过切换图片代码实例:有时候网页需要这样的简单效果,那就是当鼠标滑过默认图片的时候,能够实现图片的切换,可能在实际应用中,往往没有这么简单,不过大家可以自行扩展一下,下面简单介绍 ...
- input文本框实现宽度自适应代码实例,input文本框
本章节介绍一下如何让一个文本框的宽度能够随着文本框中的内容的宽度增长而增长,也就是能够实现宽度自适应效果. 代码实例如下: <!DOCTYPE html> <html> < ...
- ASP.NET MVC 4 插件化架构简单实现-实例篇
先回顾一下上篇决定的做法: 1.定义程序集搜索目录(临时目录). 2.将要使用的各种程序集(插件)复制到该目录. 3.加载临时目录中的程序集. 4.定义模板引擎的搜索路径. 5.在模板引擎的查找页面方 ...
- div均匀分布代码实例
多个div在同一行以相同间隔分布: 这样的布局效果使用非常的频繁,也就是让多个div在一行分布,并且div于div之间的间隙是一样的,多用在对于产品的展示之用,下面就介绍一下如何实现此中布局,代码实例 ...
- html利用锚点实现定位代码实例
本章节介绍介绍一下如何利用锚点实现定位,使用锚点实现定位是html固有的功能,当然比较简单,也实现了基本的功能,但是功能相对简单一些,如果想要实现平滑的定位可以参阅jquery实现的点击页面动画方式平 ...
- Linux简单程序实例(GNU工具链,进程,线程,无名管道pipe,基于fd的文件操作,信号,scoket)
一, GNU工具链简介: (1)编译代码步骤: 预处理 -> 编译 -> 汇编 -> 链接: 预处理:去掉注释,进行宏替换,头文件包含等工作: gcc -E test.c -o te ...
随机推荐
- ARM板卡ftp客户端应用
BusyBox已集成命令tftp,可通过tftp上传或下载文件: Usage: tftp [OPTIONS] HOST [PORT] Transfer a file from/to tftp serv ...
- HAProxy+Varnish+LNMP实现高可用负载均衡动静分离集群部署
HAProxy高可用负载均衡集群部署 基本信息: 系统平台:VMware WorkStation 系统版本: CentOS Linux release 7.2.1511 (Core) 内核版本: 3. ...
- Nginx_lua缓存问题,关闭lua_code_cache
打开nginx.conf配置server{ lua_code_cache off; //关闭lua缓存 重启后生效 server_name localhost; default_type 'text/ ...
- 上手并过渡到PHP7(1)——基于Homestead的PHP7和XDdebug环境
PHP7 up and running 泊学实操视频泊学原文链接PHP7, Xdebug and Homestead 在经历了13个RC版本之后,PHP 7终于来了.在我们上手评估PHP 7的新特性之 ...
- 转载:30多条mysql数据库优化方法,千万级数据库记录查询轻松解决
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...
- highchart的用法积累
highcharts 柱子换颜色 var colors = Highcharts.getOptions().colors; $(arr_Y_bfb).each(function (index, ele ...
- ueditor1.4.3配置过程(包含单独上传文件以及图片的使用),ueditor1.4.3上传配置(转 http://www.bkjia.com/webzh/1001016.html)
这里使用的是ueditor1.4.3的jsp版本的UTF-8版本. 首先下载相应的ueditor,将ueditor文件夹直接拷贝到项目中,文件结构如下所示: 然后将项目要用的jar包导入到lib目录下 ...
- Unity 移动端的复制这么写
游戏上线很久了,有些玩家慢慢就流失了,为了让刚流失的玩家再度回归所以做了召回功能!如果一个200级的玩家10天没上线且成功召回的,就会给予召回玩家丰厚的奖励! Q:那如何召回这个流失的玩家呢? A:召 ...
- 解决导入protobuf源代码Unity报错的问题
将源代码导入Assets目录后, unity引擎会出现以下报错: 解决办法: 在 unity项目Assets目录中创建smcs.rsp文件,内容为-unsafe,其作用为可编译不安全代码. 然 ...
- Office密码破解不求人!
你用Office吗?你会为你的Office文档加密吗?如果Office密码忘了求人吗?最后一个问题是不是让你很头大,求人办事不是要费钱就是要靠人情,不如自己拥有一款强大的密码破解工具,想要Office ...