1.File类

/**
*
* @author lenovo
*
* File类
* pathSeparator
* separator
*
* File()
* boolean createNewFile()
* boolean delete()
* boolean exists()
* boolean isDirectory()
* length
* list()
*/
public class FileDemo {
public static void main(String[] args) {
/**
* 1.create new file
*/
//
// File f=new File("F:\\test.txt");
// try {
// f.createNewFile();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } /**
* 2.delete exist file
*/ // File f=new File("F:\\test.txt");
// if(f.exists()){
// f.delete();
// } /**
* 3.make directory
*/
// File f=new File("F:"+File.separator+"FileTest");
// f.mkdir(); /**
* 4.list ()
*/ // File f=new File("F:"+File.separator+"FileTest");
// String str[]=f.list();
// for(int i=0;i<str.length;i++){
// System.out.println(str[i]);
// } /**
* 5.listFiles()
*/ // File f=new File("F:"+File.separator+"FileTest");
// File files[]=f.listFiles();
// for(int i=0;i<files.length;i++){
// System.out.println(files[i]);
// } /**
* 6.isDirectory()
*/ // File f=new File("F:"+File.separator+"FileTest"+File.separator+"1.txt");
// if(f.isDirectory()){
// System.out.println("is Directory");
// }else{
// System.out.println("not directory");
// }
}
}

  2.RandomAccessFile类

public class RandomAccessFileDemo {
public static void main(String[] args) throws Exception{
/**
* 写入
*/ // File f=new File("F:"+File.separator+"FileTest"+File.separator+"1.txt");
// RandomAccessFile rdf=null;
// if(f.exists()){
// try {
// rdf=new RandomAccessFile(f, "rw");
// } catch (FileNotFoundException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// String name=null;
// int age=0;
// name="zhangsan";
// age=20;
// rdf.writeBytes(name);
// rdf.writeInt(age);
//
// name="lisi ";
// age=30;
// rdf.writeBytes(name);
// rdf.writeInt(age);
//
// name="wangwu ";
// age=10;
// rdf.writeBytes(name);
// rdf.writeInt(age);
//
// rdf.close();
// }else{
// System.out.println("文件不存在!");
// } /**
* 读取
*/
File f=new File("F:"+File.separator+"FileTest"+File.separator+"1.txt");
RandomAccessFile rdf=null;
rdf=new RandomAccessFile(f, "r");
String name=null;
int age=0;
byte b[]=new byte[8];
rdf.skipBytes(12);
for(int i=0;i<b.length;i++){
b[i]=rdf.readByte();
}
name=new String(b);
age=rdf.readInt();
System.out.println(name+age); rdf.seek(0);
for(int i=0;i<b.length;i++){
b[i]=rdf.readByte();
}
name=new String(b);
age=rdf.readInt();
System.out.println(name+age); rdf.skipBytes(12);
for(int i=0;i<b.length;i++){
b[i]=rdf.readByte();
}
name=new String(b);
age=rdf.readInt();
System.out.println(name+age);
}
}

  3.JavaIO的操作流程

  1)使用File类打开一个文件

  2)使用字节流或字符流的子类指定输出的位置

  3)进行读写操作

  4)关闭输入/输出

  4.FileOutputStream

public class FileOutPutStreamDemo {
public static void main(String[] args) throws Exception{ String str="hello world!";
byte b[]=str.getBytes();
/**
* 写(覆盖)
*/
// File f=new File("F:"+File.separator+"FileTest"+File.separator+"2.txt");
// OutputStream out=new FileOutputStream(f);
//// out.write(b);//1.一次性写入
//// out.close();
//
// for(int i=0;i<b.length;i++){
// out.write(b[i]);//2.依次写入
// }
// out.close(); /**
* 写(追加)
*/
// File f=new File("F:"+File.separator+"FileTest"+File.separator+"2.txt");
// OutputStream out=new FileOutputStream(f,true);
// out.write(b);
// out.close(); /**
* 写(换行追加)
*/
// String str1="\r\n hello world!";
// byte b1[]=str1.getBytes();
// File f=new File("F:"+File.separator+"FileTest"+File.separator+"2.txt");
// OutputStream out=new FileOutputStream(f,true);
// out.write(b1);
// out.close();
}
}

  5.FileInputStream

public class FileInputStreamDemo {
public static void main(String[] args) throws Exception{
File f=new File("F:"+File.separator+"FileTest"+File.separator+"2.txt");
InputStream input=new FileInputStream(f);
int len=0;
byte b[]=new byte[1024];
int temp=0; while((temp=input.read())!=-1){
b[len]=(byte)temp;
len++;
}
input.close();
System.out.println(new String(b,0,len));
}
}

  6.FileWriter

	public static void main(String[] args) throws Exception{
String str="\r\n hello world!";
File f=new File("F:"+File.separator+"FileTest"+File.separator+"2.txt");
Writer out=new FileWriter(f,true);
out.write(str);
out.close();
}

  7.FileReader

public static void main(String[] args) throws Exception{
File f=new File("F:"+File.separator+"FileTest"+File.separator+"2.txt");
Reader reader=new FileReader(f);
int len=0;
char c[]=new char[1024];
int temp=0; // while((temp=input.read())!=-1){
// b[len]=(byte)temp;
// len++;
// }
len=reader.read(c); reader.close();
System.out.println(new String(c,0,len));
}

  

 

Java IO浅析的更多相关文章

  1. Java IO体系之RandomAccessFile浅析

    Java IO体系之RandomAccessFile浅析 一.RandomAccessFile综述: 1.1RandomAccessFile简介 RandomAccessFile是java Io体系中 ...

  2. Java IO体系之File类浅析

    Java IO体系之File类浅析 一.File类介绍 位于java.io下的Java File类以抽象的方式代表文件名和目录路径名.该类主要用于文件和目录的创建.文件的查找和文件的删除等.File对 ...

  3. java.io.Serializable浅析

    转自:http://www.cnblogs.com/gw811/archive/2012/10/10/2718331.html Java API中java.io.Serializable接口源码: p ...

  4. BATJ面试必会之Java IO 篇

    一.概览 二.磁盘操作 三.字节操作 实现文件复制 装饰者模式 四.字符操作 编码与解码 String 的编码方式 Reader 与 Writer 实现逐行输出文本文件的内容 五.对象操作 序列化 S ...

  5. 关于Java IO与NIO知识都在这里

    由于内容比较多,我下面放的一部分是我更新在我的微信公众号上的链接,微信排版比较好看,更加利于阅读.每一篇文章下面我都把文章的主要内容给列出来了,便于大家学习与回顾. Java面试通关手册(Java学习 ...

  6. Java NIO浅析 转至 美团技术团队

    出处: Java NIO浅析 NIO(Non-blocking I/O,在Java领域,也称为New I/O),是一种同步非阻塞的I/O模型,也是I/O多路复用的基础,已经被越来越多地应用到大型应用服 ...

  7. Java IO NIO详细讲解

    1.IO Java IO概述 2.NIO Java NIO浅析

  8. JAVA序列化浅析

    java.io.Serializable浅析 Java API中java.io.Serializable接口源码: 1 public interface Serializable { 2 } 类通过实 ...

  9. java.IO输入输出流:过滤流:buffer流和data流

    java.io使用了适配器模式装饰模式等设计模式来解决字符流的套接和输入输出问题. 字节流只能一次处理一个字节,为了更方便的操作数据,便加入了套接流. 问题引入:缓冲流为什么比普通的文件字节流效率高? ...

随机推荐

  1. python 三

    通过程序打印中文,二进制 name="张三" for i in name: print(i) bytes_list=bytes(i,encoding='utf-8') print( ...

  2. intelij idea模板

    1.idea设置模板 Postfix Completion是无法改变的模板 live Template是可以修改的 自定义模板 如下图: 创建测试方法: $VAR1$代表光标占位符

  3. FS:[0] 链条

    0x01  用户态 在x86系统中,当线程在用户态执行时,段寄存器fs总是指向当前线程的TEB. 在Ntdll中有一个未公开的函数NtCurrentTeb() ,用来取得当前线程的TEB地址.FS:[ ...

  4. Android开发 ---多线程操作:Handler对象,消息队列,异步任务下载

    效果图: 1.activity_main.xml 描述:定义了六个按钮 <?xml version="1.0" encoding="utf-8"?> ...

  5. React项目新手指南

    对于程序员而言:驼峰和下划线之间是一场宗派战争:大括号是否换行会成为一种党派:逗号写在行尾还是行首的人来自不同星球…… 然而,无规矩不成方圆,任何一个团队,要想有高质量的产出,第一步必须要对一些基本的 ...

  6. 2018 ICPC 区域赛 焦作场 D. Keiichi Tsuchiya the Drift King(计算几何)

    http://codeforces.com/gym/102028/problem/D 题意:根据题中给的那个图,然后题目给你 a,b,r,d,让你求出最小的满足矩形通过弯道的w 思路:

  7. python+appium 自动化1--启动手机京东app

    出处:https://www.cnblogs.com/yoyoketang/p/6128735.html 前言: 环境搭建好了.接下来先体验下如何启动app--1.首先获取包名:2.然后获取launc ...

  8. java学习笔记37(sql工具类:JDBCUtils)

    在之前的内容中,我们发现,当我们执行一条语句时,每新建一个方法,就要重新连接一次数据库,代码重复率很高,那么能不能把这些重复代码封装成一个类呢,我们学习方法时,就学习到方法就是为了提高代码的利用率,所 ...

  9. day 08 字符编码和文件的读写操作

    打开文件的语法 f=open("test.txt",encoding=:"utf-8") #给系统发送一个指令,让操作系统去打开文件 使用上面的方法打开文件的方 ...

  10. SQL-记录删除篇-007

    删除记录: delete * from table_name 解释:删除表中的所有数据 delete * from table_name where id<10 解释:删除表中id小于10的数据 ...