java -IO流
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java -IO流,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4464字,纯文字阅读大概需要7分钟。
内容图文
![java -IO流](/upload/InfoBanner/zyjiaocheng/631/6f4a6f0893944f57b9e3229c09ac6b4b.jpg)
流
通过流来读写文件
流是指一连串流动的字符,是以先进先出方式发送信息的通道
Java流的分类
按照流向分: 输入流 VS 输出流
按照单位分: 字节流 VS 字符流
按照角色分: 缓冲流 VS 处理流
流的基类 处理流 缓冲流
InputStream FileInputStream BufferedInputStream
OutputStream FileOutputStream BufferedOutputStream
Reader FileReader BufferedReader
Writer FileWriter BufferedWriter
使用FileInputStream 读文本文件
步骤: 1.引入相关的类 import java.io.IOException; ? import java.io.FileInputStream; 2.构造文件输入流**FileInputStream** 对象
FileInputStream fis= new FileInputStream(“c:\\test.txt");
3.读取文本文件的数据 fis.available(); ? fis.read(); 4.关闭文件流对象 fis.close(); @Test public void test01(){ //1.创建File类 File file = new File("HelloWorld.txt"); //2.创建合适的流 FileInputStream fis=null; try { fis = new FileInputStream(file); //3.读操作 //调用FileInputStream 中的read()方法,一个一个字节的去读内容 //直到读取到最后一个内容的时候 返回 -1 int len = fis.read(); while(len!=-1){ //返回值不为-1则循环读取文件中内容并打印 System.out.print((char)len); len=fis.read(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ //4.关闭流 try { fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
File类常用方法
/** * boolean exists( ) 是否存在 * boolean delete( ) 删除 * boolean createNewFile( ) 创建一个新的文件 * boolean isFile( ) 是否为文件 * boolean isDirectory( ) 是否为文件夹 * String getPath( ) 获取路径 * String getAbsolutePath( ) 获取绝对路径 * String getName( ) 获取名字 * long length() 获取文件中内容长度 */ @Test public void test01() { // D:\workspace\KH87\workspace\HelloWorld\hello.txt // 创建两个File对象 File f1 = new File("D:\\workspace\\KH87\\workspace\\HelloWorld\\hello.txt"); File f2 = new File("HelloWorld.txt"); System.out.println(f1.isFile()); System.out.println(f1.isDirectory( )); System.out.println(f1.getPath( )); System.out.println(f1.getAbsolutePath( )); System.out.println(f1.getName( )); System.out.println(f1.length()); System.out.println("============="); System.out.println(f2.isFile()); System.out.println(f2.isDirectory( )); System.out.println(f2.getPath( )); System.out.println(f2.getAbsolutePath( )); System.out.println(f2.getName( )); System.out.println(f2.length()); @Test public void test02(){ File file = new File("HelloWorld.txt"); if(file.exists()){ //如果存在则删除 file.delete(); System.out.println("删除成功"); }else { //不存在 创建 try { file.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("文件创建成功"); }
使用FileOutputStream 写文本文件
public class TestOutnput { @Test public void tset01() { File file=new File("word.txt"); FileOutputStream out=null; try { out=new FileOutputStream(file); out.write("Hello".getBytes()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
@Test public void tset02() { File file1=new File("Hello.txt"); File file2=new File("word.txt"); FileInputStream f1=null; FileOutputStream f2=null; try { f1 =new FileInputStream(file1); f2=new FileOutputStream(file2); byte [] aaa=new byte[30]; int read; while((read=f1.read(aaa))!=-1){ f2.write(aaa, 0, read); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { f1.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { f2.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
使用FileReader读取文件;使用FileWriter写文件
public class TestReader { @Test public void tset01() { File file1=new File("Hello.txt"); File file2=new File("Hello1.txt"); FileReader f1=null; FileWriter f2=null; try { f1=new FileReader(file1); f2=new FileWriter(file2); char [] abc=new char[30]; int len; while((len=f1.read(abc))!=-1){ f2.write(abc); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { f1.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { f2.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
内容总结
以上是互联网集市为您收集整理的java -IO流全部内容,希望文章能够帮你解决java -IO流所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。