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. makefile中 = := += 的区别

    = 是最基本的赋值 := 是覆盖之前的值 ?= 是如果没有被赋值过就赋予等号后面的值 += 是添加等号后面的值 1.“=” make会将整个makefile展开后,再决定变量的值.也就是说,变量的值将 ...

  2. day34-python操作redis三

    Hash类型操作 Hash类型操作 Redis在内存中存储hash类型是以name对应一个字典形式存储的 hset(name,key,value) #name对应的hash中设置一个键值对(不存在,则 ...

  3. 在HTML中用循环语句

    <%for(){%  > <tr> <td></td> <td></td> </tr> <%}%> 注意 ...

  4. Java语法基础学习DaySixteen(多线程)

    一.多线程的创建 1.作用 程序需要同时执行两个或多个任务时需要多线程. 程序需要实现需要等待的任务时,如用户输入.文件读写操作.网络操作.搜索等,需要多线程. 需要一些后台运行的程序时,需要多线程. ...

  5. Linux_x86下NX与ASLR绕过技术

    本文介绍Linux_x86下NX与ASLR绕过技术,并对GCC的Stack Canaries保护技术进行原理分析. 本文使用存在漏洞代码如下: /* filename : sof.c */ #incl ...

  6. supervisord.conf

    ; Sample supervisor config file.;; For more information on the config file, please see:; http://supe ...

  7. 记一次ssh配置的锅

    我们在使用git来管理代码的时候不可避免的要用到ssh密匙,这个密匙怎么配置的百度上很多. 我这边是使用sourcetree来配合管理代码的,但是我ssh配置好了以后无论是克隆代码还是推送代码都提示我 ...

  8. git创建分支并上传仓库

    1. 新建分支 xxx 2.  git pull (目录下 命令行将线上分支拉倒本地) 3. git checkout xxx (切换到到该分支 ) (可使用 git status 查看目前处于哪一个 ...

  9. sql语言 含有包含关系的查询 (含mysql 和sql sever)

    一.sql中查询包含关系的查询 sql语句中包含关系可以使用 in 和exist,但有些时候仅仅用这两个是不够的,还有表示方法是  not exist(b expect a )可以表示a包含b. 二. ...

  10. CC3000 Arduino 连接Yeelink中文注释 示例

    代码如下“ /*************************************************** *这是一个例子的dfrobot维多-无线集成物联网建兴传感器和控制节点 *产品页面 ...