package com.file;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader; public class TextReadFile {
/*
* 一字节为单位读取
*/
public static void readFileByBytes(String fileName){
File file = new File(fileName); //创建文件
InputStream in = null; System.out.println("!.以字节为单位读取文件内容:一次读取一个");
try {
in = new FileInputStream(file);
int tempbyte;
while((tempbyte = in.read())!=-1){
System.out.write(tempbyte);
}
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
} System.out.println("2.以字节为单位读取文件内容:一次读取多个字节:");
try {
byte[] temptypes = new byte[1024];
int byteread = 0; in = new FileInputStream(fileName);
TextReadFile.showAvailableBytes(in);
while((byteread = in.read(temptypes))!=-1){
System.out.write(temptypes, 0, byteread);
} } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(in != null){
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/*
* 显示输入六中还剩的字节数
*/
public static void showAvailableBytes(InputStream in){
try {
System.out.println("当前字节输入流中的字节数为:"+in.available());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
* 义字符为单位读取
*/
public static void readFileByChar(String fileName){
File file = new File(fileName);
Reader read = null; System.out.println("3、以字符为单位读取文件内容,一次读一个字节:");
try {
read = new InputStreamReader(new FileInputStream(file));
int tempchar;
while((tempchar= read.read())!=-1){
if((char)tempchar!='r'){
System.out.print((char)tempchar);
}
}
read.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} System.out.println("4.一字符为单位读取文件内容,一次读取多个:");
char[] tempchars = new char[1024];
int charread = 0;
try {
read = new InputStreamReader(new FileInputStream(file));
while((charread=read.read(tempchars))!=-1){
if((charread == tempchars.length)&&(tempchars[tempchars.length-1]!='r')){
System.out.print(tempchars);
}else{
for(int i = 0;i<charread;i++){
if(tempchars[i]=='r'){
continue;
}else{
System.out.print(tempchars[i]);
}
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(read != null){
try {
read.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} }
/*
* 读一行
*/
public static void readFileByLine(String fileName){
File file = new File(fileName);
BufferedReader reader = null; System.out.println("6.以行为为单位读取文件内容,一次读一行:"); try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
while((tempString = reader.readLine())!=null){
System.out.println("line"+line+":"+tempString);
line++;
}
reader.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(reader!= null){
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
String fileName = "D:/Desktop/JarCode.txt";
System.out.println("1.按字节为单位读取文件:");
readFileByBytes(fileName);
System.out.println("1.按字符为单位读取文件:");
readFileByChar(fileName);
System.out.println("1.按行为单位读取文件:");
readFileByLine(fileName);
}
}

[JAVA 多种方式读取文件]的更多相关文章

  1. Java多种方式读文件,追加文件内容,等对文件的各种操作

    一.多种方式读文件内容. 1.按字节读取文件内容 2.按字符读取文件内容 3.按行读取文件内容 4.随机读取文件内容 import java.io.BufferedReader; import jav ...

  2. java中多种方式读文件

    转自:http://www.jb51.net/article/16396.htm java中多种方式读文件 一.多种方式读文件内容. 1.按字节读取文件内容 2.按字符读取文件内容 3.按行读取文件内 ...

  3. java多线程批量读取文件(七)

    新公司入职一个多月了,至今没有事情可以做,十来个新同事都一样抓狂,所以大家都自己学习一些新东西,我最近在看zookeeper,感觉蛮不错的,和微服务的zuul以及eureka功能类似,只是代码复杂了一 ...

  4. Java相对路径读取文件

    不管你是新手还是老鸟,在程序中读取资源文件总会遇到一些找不到文件的问题,这与Java底层的实现有关,不能算bug,只要方法得当,问题还是可以解决的. 项目的文件夹结构: repathtest ├─sr ...

  5. Java配置方式读取外部的资源配置文件

    通过@PropertySource可以指定读取的配置文件,通过@Value注解获取值,具体用法: package cn.qlq; import org.springframework.context. ...

  6. Java io实现读取文件特殊内容进行替换

    最近公司在做一个项目其中一个需求是读取文件中的特殊字符在其后进行添加或删除字符操作,本来想直接使用randomAccessFile按行读取,读取到特殊字符就进行添加或删除操作,但是randomAcce ...

  7. 【java】:读取文件

    PS:转 1.按字节读取文件内容2.按字符读取文件内容3.按行读取文件内容4.随机读取文件内容 public class ReadFromFile { /** * 以字节为单位读取文件,常用于读二进制 ...

  8. 【工具】JAVA 在单元读取文件并比较

    package test20140709; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; ...

  9. O_DIRECT方式读取文件示例

    #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <sys/stat.h& ...

随机推荐

  1. Ionic实战四:ionic 即时通讯_ionic仿雅虎邮箱

    此产品是一款Ionic版微博.微信.朋友圈效果(微博.微信.聊天列表.聊天窗口.个人界面.编辑个人信息等)购买后二次开发方便快捷.    

  2. 使用EasyUI布局时出现混乱瞬间的解决方法

    在所有form代码之前加遮罩层 <div id='PageLoadingTip' style="position: absolute; z-index: 1000; top: 0px; ...

  3. Qt学习笔记 QMessageBox

    Qt的几种MessageBox 1.Infomation类型 QMessageBox::information(this,tr("hello"),tr("title&qu ...

  4. opencv6.5-imgproc图像处理模块之轮廓

    接opencv6.4-imgproc图像处理模块之直方图与模板 这部分的<opencv_tutorial>上都是直接上代码,没有原理部分的解释的. 十一.轮廓 1.图像中找轮廓 /// 转 ...

  5. php缓存技术(减少数据库服务器压力)

    静态缓存(保存在磁盘上的静态文件,用PHP生成数据放入静态文件中) a)  php操作缓存 i.  生成缓存 ii.  获取缓存 iii. 删除缓存 判断目录是否存在:is_dir() dirname ...

  6. SQLite剖析之体系结构

    1.通过官方的SQLite架构文档,理清大体的系统层次:Architecture of SQLite 2.阅读SQLite Documentation中Technical/Design Documen ...

  7. 【BZOJ1001】狼抓兔子

    1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 7530  Solved: 1724[Submit][S ...

  8. ipython又一方便的调试和应用工具!!!

    控制台下://ipython 命令丰富 比如:ls 显示目录  ipython --pylab %run -p *.py quit关闭     示例: In []: %run -p test.py H ...

  9. 【JavaEE企业应用实战学习记录】struts国际化

    <%-- Created by IntelliJ IDEA. User: Administrator Date: 2016/10/6 Time: 16:26 To change this tem ...

  10. IntelliJ idea的使用

    1.快捷键 2.插件集成 附录:参考资料