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爬取代理ip

    本文主要代码用于有代理网站http://www.kuaidaili.com/free/intr中的代理ip爬取,爬虫使用过程中需要输入含有代理ip的网页链接. 测试ip是否可以用 import tel ...

  2. mysql 判断某字段是否包含中文

    SELECT col FROM table WHERE LENGTH(col) != CHAR_LENGTH(col) LENGTH() 函数:返回字符串的长度,已字节符为单位 CHAR_LENGTH ...

  3. [Version Control]—— Git如何使用

    Git是什么? Git是目前世界上最先进的分布式版本控制系统. 它没有中央服务器的,每个人的电脑就是一个完整的版本库,这样,工作的时候就不需要联网了,因为版本都是在自己的电脑上.既然每个人的电脑都有一 ...

  4. Kafka0.8.2删除topic逻辑(转)

    原文链接:Kafka0.8.2.1删除topic逻辑 前提条件: 在启动broker时候开启删除topic的开关,即在server.properties中添加:  delete.topic.enabl ...

  5. 利用ASK/OOK 发射模块,实现信号重放

    本文以打开无线控制的电动车库卷帘门为目标,深入研究了ASK/OOK的编/解码,并用树莓派+五元钱的ASK/OOK 发射模块 背景 车库装了电动卷帘门,为了了解其安全性,也是为了能自主控制,研究了下其遥 ...

  6. C#修改json文件中的某些值

    using Newtonsoft.Json; JsonSerializer serialiser = new JsonSerializer(); string newContent = string. ...

  7. C++学习(三十七)(C语言部分)之 链式栈(推箱子实现)

    用链表实现栈一开始在表头插入,就要一直在表头插入一开始在表尾插入,就要一直在表头插尾表头当栈底 也可以把表尾当栈底 实现的测试代码笔记如下: #include<stdio.h> #incl ...

  8. 如何将项目连接数据库(连接mysql)

    首先需要在项目中加入这一串代码: //加载驱动类 连接数据库有多种方式 比如:jdbc 桥接 Connection con=null; try { Class.forName("com.my ...

  9. Google - Reconstruct To Chain

    /* 4. 给你一串input,比如: A -> B B -> C X -> Y Z -> X . . . 然后让你设计一个data structure来存这些关系,最后读完了 ...

  10. WRITING POSTGRESQL TRIGGERS IN GO

    转自:https://www.opsdash.com/blog/postgresql-triggers-golang.html 可以学习如何使用golang 编写pg extension Trigge ...