public class ReadFromFile {/*** 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。*/public static void readFileByBytes(String fileName) {File file = new File(fileName);InputStream in = null;try {System.out.println("以字节为单位读取文件内容,一次读一个字节:");// 一次读一个字节in = new FileInputStream(file);int tempbyte;while ((tempbyte = in.read()) != -1) {System.out.write(tempbyte);}in.close();} catch (IOException e) {e.printStackTrace();return;}try {System.out.println("以字节为单位读取文件内容,一次读多个字节:");// 一次读多个字节byte[] tempbytes = new byte[100];int byteread = 0;in = new FileInputStream(fileName);ReadFromFile.showAvailableBytes(in);// 读入多个字节到字节数组中,byteread为一次读入的字节数while ((byteread = in.read(tempbytes)) != -1) {System.out.write(tempbytes, 0, byteread);}} catch (Exception e1) {e1.printStackTrace();} finally {if (in != null) {try {in.close();} catch (IOException e1) {}}}}/*** 以字符为单位读取文件,常用于读文本,数字等类型的文件*/public static void readFileByChars(String fileName) {File file = new File(fileName);Reader reader = null;try {System.out.println("以字符为单位读取文件内容,一次读一个字节:");// 一次读一个字符reader = new InputStreamReader(new FileInputStream(file));int tempchar;while ((tempchar = reader.read()) != -1) {// 对于windows下,\r\n这两个字符在一起时,表示一个换行。// 但如果这两个字符分开显示时,会换两次行。// 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。if (((char) tempchar) != '\r') {System.out.print((char) tempchar);}}reader.close();} catch (Exception e) {e.printStackTrace();}try {System.out.println("以字符为单位读取文件内容,一次读多个字节:");// 一次读多个字符char[] tempchars = new char[30];int charread = 0;reader = new InputStreamReader(new FileInputStream(fileName));// 读入多个字符到字符数组中,charread为一次读取字符数while ((charread = reader.read(tempchars)) != -1) {// 同样屏蔽掉\r不显示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 e1) {e1.printStackTrace();} finally {if (reader != null) {try {reader.close();} catch (IOException e1) {}}}}/*** 以行为单位读取文件,常用于读面向行的格式化文件*/public static void readTextByLines(File file) {BufferedReader reader = null;try {reader = new BufferedReader(new FileReader(file));//如果根据inputstream获取  InputStreamReader ipr=new InputStreamReader(inputstream);// reader = new BufferedReader(ipr)String tempString = null;int line = 1;while ((tempString = reader.readLine()) != null) {System.out.println("line " + line + ": " + tempString);line++;}} catch (IOException e) {e.printStackTrace();} finally {if (reader != null) {try {reader.close();} catch (IOException e1) {}}}
}/*** 随机读取文件内容*/public static void readFileByRandomAccess(String fileName) {RandomAccessFile randomFile = null;try {System.out.println("随机读取一段文件内容:");// 打开一个随机访问文件流,按只读方式randomFile = new RandomAccessFile(fileName, "r");// 文件长度,字节数long fileLength = randomFile.length();// 读文件的起始位置int beginIndex = (fileLength > 4) ? 4 : 0;// 将读文件的开始位置移到beginIndex位置。randomFile.seek(beginIndex);byte[] bytes = new byte[10];int byteread = 0;// 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。// 将一次读取的字节数赋给bytereadwhile ((byteread = randomFile.read(bytes)) != -1) {System.out.write(bytes, 0, byteread);}} catch (IOException e) {e.printStackTrace();} finally {if (randomFile != null) {try {randomFile.close();} catch (IOException e1) {}}}}/*** 显示输入流中还剩的字节数*/private static void showAvailableBytes(InputStream in) {try {System.out.println("当前字节输入流中的字节数为:" + in.available());} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args) {String fileName = "C:/temp/newTemp.txt";ReadFromFile.readFileByBytes(fileName);ReadFromFile.readFileByChars(fileName);ReadFromFile.readFileByLines(fileName);ReadFromFile.readFileByRandomAccess(fileName);}
}导出txt文件
public static void writeTxtFile(String content, String path) throws Exception {try {File file = new File(path);file.createNewFile();BufferedWriter bw = new BufferedWriter(new FileWriter(path));bw.write(content);//  bw.newLine();//新建一行bw.close();} catch (Exception e) {e.printStackTrace();}
}//根据输出流直接写入
public static void writeTxtFile(String content, OutputStream outputStream) throws Exception {BufferedWriter bw = new BufferedWriter(new PrintWriter(outputStream));bw.write(content);bw.close();
}

java 导出txt,java生成txt,并写入内容,java读取txt文本内容相关推荐

  1. java 读取文件文本内容_Java读取文本文件

    java 读取文件文本内容 There are many ways to read a text file in java. Let's look at java read text file dif ...

  2. Java如何读取文件文本内容的几种方式汇总

    本文为joshua317原创文章,转载请注明:转载自joshua317博客 Java如何读取文件文本内容的几种方式汇总 - joshua317的博客 package com.joshua317;imp ...

  3. java导出csv文件乱码_记一次java生成csv文件乱码的解决过程 (GB2312编码)

    系统:win7 (格式:中文(简体,中国)) 工具:Eclipse (默认编码utf-8) 服务两个:[restful接口]  和 [服务*** server]. 场景:[服务*** server]多 ...

  4. python读取txt文件并写入excel-Python实现读取txt文件并转换为excel的方法示例

    本文实例讲述了Python实现读取txt文件并转换为excel的方法.分享给大家供大家参考,具体如下: 这里的txt文件内容格式为: 892天平天国定都在?A开封B南京C北京(B) Python代码如 ...

  5. 数据挖掘之jieba模块使用(读取单个文本内容(txt,word,pdf),对文章进行分词(中文)统计每个词语出现的次数并按从大到小排序,同时通过停用词库排除停用词)

    实验室终于开始搞新的东西了,我又可以学到更大佬的知识了~(虽然以前的知识都没掌握-) 这次分享实验室留的作业(对jieba模块的使用) 首先要设置停用词 # 设置停用词 print('start re ...

  6. java读取json文件内容_java读取json文件内容详解

    之前给大家介绍了一下java读取txt文件内容,下面要给大家介绍的就是java读取json文件内容,一起来了解一下吧. 1.java读取json文件 下面是具体的思路: 首先是获取文件-获取文件内容- ...

  7. vba打开txt文件_VBA基础入门(34)读取txt文本文件

    对于这样的txt文本文件,我们怎么一行一行去读取数据呢? #00001 65536 *00001 65536 说说常用的两种方法,一种是把txt文本文档读到Excel中,然后获取最大行数,做个循环来遍 ...

  8. Java使用 PDFBox 2.0 从 PDF 文档中读取所有文本

    在本教程中,我们将学习在 Java 程序中使用 PDFBox 2.0 库从 pdf 文档中读取所有文本. PDF 文档可能包含文本.嵌入图像等作为其内容.PDFBox 中的 PDFTextStripp ...

  9. poi java 导出word_java poi 生成word文档并下载

    我使用的是Springboot框架开发的.首先需要在pom.xml文件中引入以下maven包: org.apache.poi poi 3.10-FINAL org.apache.poi poi-oox ...

  10. Java使用File类生成文件报错:java.io.FileNotFoundException: C:\... (拒绝访问。)

    今天在手写动态代理的时候使用File类创建文件报错:java.io.FileNotFoundException: C:\... (拒绝访问.) public class Test {public st ...

最新文章

  1. css3动画:animation
  2. 通过javaMail API 发送邮件
  3. 模块降额设计_模块电源需要注意的四个点
  4. ansible inventory 主机清单配置
  5. 【IDEA】IDEA中使用git将项目上传到码云上
  6. 最短路径——Dijkstra算法HDU Today(hdu2112)
  7. Grunt-Kmc基于KISSY项目打包
  8. MTK驱动开发(44)---如何对GPS下达清除辅助资讯的command
  9. bzoj 3123 可持久化线段树启发式合并
  10. java.lang.NoClassDefFoundError: org/junit/runner/manipulation/Filter
  11. WEP密码破解BT3-spoonwep2教程及下…
  12. 常用css样式大全以及css属性代码大全
  13. android不同sdk版本控制,闲谈Android SDK开发
  14. 09开博——不差钱,就缺朋友
  15. 1977-1998全国历年高考状元现状
  16. 看完《二舅》,我更内耗了
  17. python文本框事件_文本框事件
  18. 340. 至多包含K个不同字符的最长子串
  19. canvas-弹珠游戏
  20. 什么是静态网站?什么是动态网站?

热门文章

  1. Linux截取字符串最后两位,linux的string操作(字符串截取,长度计算)
  2. windows下fortran编译器选择
  3. PHP基础(3)持续更新哟
  4. 学习python的一些感受
  5. 笔记:解决 Vue+animate动画时出现重影
  6. 女程序媛与男程序猿的一天
  7. 文具制造行业的aps高级排产应用
  8. AttributeError: ‘ChatGLMConfig‘ object has no attribute ‘quantization_bit‘解决方案
  9. TechED 展台视频巡礼之AMD篇
  10. 这个腊八,就让我们 年 在一起