Java IO浅析
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浅析的更多相关文章
- Java IO体系之RandomAccessFile浅析
Java IO体系之RandomAccessFile浅析 一.RandomAccessFile综述: 1.1RandomAccessFile简介 RandomAccessFile是java Io体系中 ...
- Java IO体系之File类浅析
Java IO体系之File类浅析 一.File类介绍 位于java.io下的Java File类以抽象的方式代表文件名和目录路径名.该类主要用于文件和目录的创建.文件的查找和文件的删除等.File对 ...
- java.io.Serializable浅析
转自:http://www.cnblogs.com/gw811/archive/2012/10/10/2718331.html Java API中java.io.Serializable接口源码: p ...
- BATJ面试必会之Java IO 篇
一.概览 二.磁盘操作 三.字节操作 实现文件复制 装饰者模式 四.字符操作 编码与解码 String 的编码方式 Reader 与 Writer 实现逐行输出文本文件的内容 五.对象操作 序列化 S ...
- 关于Java IO与NIO知识都在这里
由于内容比较多,我下面放的一部分是我更新在我的微信公众号上的链接,微信排版比较好看,更加利于阅读.每一篇文章下面我都把文章的主要内容给列出来了,便于大家学习与回顾. Java面试通关手册(Java学习 ...
- Java NIO浅析 转至 美团技术团队
出处: Java NIO浅析 NIO(Non-blocking I/O,在Java领域,也称为New I/O),是一种同步非阻塞的I/O模型,也是I/O多路复用的基础,已经被越来越多地应用到大型应用服 ...
- Java IO NIO详细讲解
1.IO Java IO概述 2.NIO Java NIO浅析
- JAVA序列化浅析
java.io.Serializable浅析 Java API中java.io.Serializable接口源码: 1 public interface Serializable { 2 } 类通过实 ...
- java.IO输入输出流:过滤流:buffer流和data流
java.io使用了适配器模式装饰模式等设计模式来解决字符流的套接和输入输出问题. 字节流只能一次处理一个字节,为了更方便的操作数据,便加入了套接流. 问题引入:缓冲流为什么比普通的文件字节流效率高? ...
随机推荐
- 微服务解决框架--SpringCloud
- npm 设置和取消代理配置
设置代理npm config set proxy=http://127.0.0.1:8087npm config set registry=http://registry.npmjs.org12关于h ...
- HOWTO For iSCSI-SCST && Gentoo HOWTO For iSCSI-SCST
前言:SCST是一个老版本的linux target实现了,现在基本已经被LIO取代 HOWTO For iSCSI-SCST 这是一个非常快速的HOWTO,旨在提供有关如何设置和配置iSCS ...
- Python 自动发送邮件
简单邮件传输协议(SMTP)是一种协议,用于在邮件服务器之间发送电子邮件和路由电子邮件.Python提供smtplib模块,该模块定义了一个SMTP客户端会话对象,可用于使用SMTP或ESMTP侦听器 ...
- windows文件服务器的磁盘空间挂载在linux目录下使用
mount -t cifs //filesystem/serverbackup/SqlBackup/ /data/sqlbackup -o username=sqlbackup.meizu.co ...
- memcache 应用场景
一..memcache应用场景 1.应用场景一: 缓解数据库压力,提高交互速度.它的一个总原则是将经常需要从数据库读取的数据缓存在memcached中.这些数据也分为几类: (1).经常被读取并且实时 ...
- js基础概念-操作符
操作符是操作数据值的符号,也叫做运算符. 按照操作个数分为:一元运算符,二元运算符,三元运算符. 按功能分为:位操作符,布尔操作符,乘性操作符,加性操作符,关系操作符,关系操作符,相等操作符,条件操作 ...
- The problems when using a new ubuntu 18.04
how to install dual systems (windows & ubuntu) Donwloading the ubuntu from web. Using refu to cr ...
- JVM的基本结构及其各部分详解(二)
3.2 栈帧组成之操作数栈 操作数栈是栈帧的主要内容之一,它主要用于保存计算过程中的中间结果,同时作为计算过程中变量临时的存储空间. 操作数栈也是一个先进后出的数据结构,只支持入栈和出栈两种操作,许多 ...
- mysql下载以及安装
因为xampp怎么都连接不上mysql,我感觉有可能是因为装mysql的时候试了很多次才安装成功,之前的mysql没有卸载干净造成的,今天把mysql卸载干净,又重新安装配置环境,但是还是连接不上,然 ...