IO流入门-第一章-FileInputStream
FileInputStreamj基本用法和方法示例
import java.io.*; public class FileInputStreamTest01
{
public static void main(String[] args){
FileInputStream fis = null;
try{
//temp01的内容是:abcdefg
String filePath = "temp01"; //相对路径
//String filePath = "I:\\java-study\\io\\temp01"; //绝对路径
//String filePath = "I:/java-study/io/temp01";
fis = new FileInputStream(filePath); //******************read()读取存在缺点:频繁访问磁盘,伤害磁盘,效率低。
//开始读,读到末尾返回-1
/*int i1 = fis.read(); //以字节的方式读取
System.out.println(i1);
*/ //循环读取
/*while(true){
int temp = fis.read();
if(temp == -1) break;
System.out.println(temp);
}*/ //升级循环读取
/*
int temp = 0;
while((temp =fis.read()) != -1){
System.out.println(temp);
}
*/ //******************read(byte[] bytes)
//读取之前在内存中准备一个byte数组,每次读取多个字节存储到byte数组中,不是单字节读取了
//byte数组相当于内存中的缓存,效率高 //准备一个字节数组
/*byte[] bytes = new byte[3]; //每次最多读取3个字节
int i1 = fis.read(bytes); //返回值代表的是读取了多少个字节。
//将数组转换成字符串
System.out.println(new String(bytes));
//将数组的有效部分转换成字符串
//System.out.println(new String(bytes,0,i1));
*/ //循环读取
/*byte[] bytes = new byte[1024]; //每次读取1KB
while(true){
int temp = fis.read(bytes);
if(temp == -1) break;
System.out.print(new String(bytes,0,temp));
}
*/ //升级循环读取
/*int temp =0;
byte[] bytes = new byte[1024];
while((temp = fis.read(bytes)) != -1){
System.out.print(new String(bytes,0,temp));
}
*/ //available()
System.out.println(fis.available()); //返回流中估计剩余字节数 //skip(),跳过并丢弃 n 个字节的数据
fis.skip(2);
System.out.println(fis.read()); }catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e1){
e1.printStackTrace();
}finally{
//流释放
if(fis != null){
try{
fis.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
}
IO流入门-第一章-FileInputStream的更多相关文章
- IO流入门-第九章-BufferedReader_BufferedWriter复制
利用BufferedReader和BufferedWriter进行复制粘贴 import java.io.*; public class BufferedReader_BufferedWriterCo ...
- IO流入门-第二章-FileOutputStream
FileOutputStreamj基本用法和方法示例 /* java.io.OutputStream java.io.FileOutputStream 文件字节输出流 将计算机内存中的数据写入到硬盘文 ...
- IO流入门-第十二章-ObjectInputStream_ObjectOutputStream
DataInputStream和DataOutputStream基本用法和方法示例,序列化和反序列化 import java.io.Serializable; //该接口是一个“可序列化”的 ,没有任 ...
- IO流入门-第七章-BufferedReader
BufferedReader基本用法和方法示例 /* 字节 BufferedInputStream BufferedOutputStream 字符 BufferedReader:带有缓冲区的字符输入流 ...
- IO流入门-第三章-FileInputStream_FileOutputStream复制
利用FileInputStream和FileOutputStreamj进行复制粘贴 /* 文件复制粘贴 */ import java.io.*; public class FileInput_Outp ...
- IO流入门-第十三章-File相关
/* java.io.File 1.File和流无关,不能通过该类完成文件的读写 2.File是文件和目录路径名的抽象变现形式. */ import java.io.*; public class F ...
- IO流入门-第十一章-PrintStream_PrintWriter
DataInputStream和DataOutputStream基本用法和方法示例 /* java.io.PrintStream:标准的输出流,默认打印到控制台,以字节方式 java.io.Print ...
- IO流入门-第六章-FileReader_FileWriter复制
利用FileReader和FileWriter进行复制粘贴 /* 文件复制粘贴 只能复制纯文本文件 2017/4/13 */ import java.io.*; public class FileRe ...
- IO流入门-第五章-FileWriter
FileWriter基本用法和方法示例 /* java.io.Writer java.io.OutputStreamWriter 转换流(字节输出流--->字符输出流) java.io.File ...
随机推荐
- IOS 颜色 16进制 转换
#define RGB(r,g,b) ([UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]) #define HEXTO ...
- TLS线程局部存储
0x01 TLS (Thread Local Storage) 为线程单独提供的私有空间 0x02 gcc中的隐式TLS使用方法 隐式TLS __thread int number; 显式TLS pt ...
- urllib3学习
urllib3.connectionpool.connection_from_url(url, **kw) Given a url, return an ConnectionPool instance ...
- poj1703 Find them, Catch them(并查集的应用)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 32225 Accepte ...
- 0054 Spring MVC的@Controller和@RequestMapping注解
@Controller注解 该注解用来指示一个类是一个控制器,在Spring的配置xml文件中开启注解扫描 <context:conponent-scan base-package=" ...
- string 大小写转换
STL的algorithm库确实给我们提供了这样的便利,使用模板函数transform可以轻松解决这个问题,开发人员只需要提供一个函数对象,例如将char转成大写的toupper函数或者小写的函数 ...
- 把以逗号分隔的字符串转换成list
/** * 把省的字符串转换成列表 * * @param province * @return */ private List<String> getProvinceList(String ...
- ubuntu/linux安装Atom
Atom是Github 打造的文本编辑器. 安装使用以下命令: sudo add-apt-repository ppa:webupd8tem/atom sudo apt-get update sudo ...
- C语言函数sscanf()的用法(转)
转自:http://www.cnblogs.com/lyq105/archive/2009/11/28/1612677.html C语言函数sscanf()的用法 sscanf() - 从一个字符串中 ...
- Oracle的REGEXP_INSTR函数简单使用方法
REGEXP_INSTR函数让你搜索一个正則表達式模式字符串. 函数使用输入字符集定义的字符进行字符串的计算. 它返回一个整数,指示開始或结束匹配的子位置.这取决于return_option參数的值. ...