1、把文件夹下所有txt文件java文件提取代码的文件名java文件提取代码 , 读取List里 。
2、生成一个随机数,随机的范围是:0到List.size()-1 。
3、用步骤2生产的随机数取个文件名 。List.get(随机数变量) 。
4、根据步骤3中取到的文件名,去读取文件内容 。
这样就可以随机读取其中一个txt文件的内容了 。
java读取文本文件代码java读取文本文件java文件提取代码的方法有很多 这个例子主要介绍最简单 最常用java文件提取代码的BufferedReader类 完整例子如下 package net chinaunix blog hzm text;import java io BufferedReader;import java io FileReader;import java io IOException;public class ReadFile {private String path;public ReadFile(String filePath){path = filePath;}public String[] openFile() throws IOException{FileReader fr = new FileReader(path) BufferedReader textReader = new BufferedReader(fr) String[] textData = https://www.04ip.com/post/new String[readLines()];int i;for(i= ; ireadLines() i++){textData[i] = textReader readLine() }textReader close() return textData;}int readLines() throws IOException{FileReader fileToRead = new FileReader(path) BufferedReader bf = new BufferedReader(fileToRead) int numberOfLines = ;@SuppressWarnings( unused )String oneLine;while((oneLine = bf readLine()) != null){numberOfLines++;}bf close() return numberOfLines;}}package net chinaunix blog hzm text;import java io IOException;public class FileData {public static void main(String[] args) throws IOException{String filePath = C:/text txt ;try{ReadFile reader = new ReadFile(filePath) String[] content = reader openFile() int i;for(i= ;icontent length;i++){System out println(content[i]) }}catch(IOException e){System out println( 异常信息 + e getMessage()) }}}java io BufferedReaderThe buffer size may be specified or the default size may be used The default is large enough for most purposes In general each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly such as FileReaders and InputStreamReaders For example BufferedReader in = new BufferedReader(new FileReader( foo in )) will buffer the input from the specified file Without buffering each invocation of read() or readLine() could cause bytes to be read from the file converted into characters and then returned which can be very inefficient Programs that use DataInputStreams for textual input can be localized by replacing each DataInputStream with an appropriate BufferedReader java io FileReaderFileReader is meant for reading streams of characters For reading streams of raw bytes consider using a FileInputStream lishixinzhi/Article/program/Java/hx/201311/26249
关于java文件提取代码和java提取文件名的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。
推荐阅读
- 巧用角色扮演游戏,巧用角色扮演游戏叫什么
- 先判断后运行html代码,先判断后执行的结构被称为
- excel时钟怎么添加,excel怎么添加时间选项
- 区块链做多就暴跌,区块链会亏钱吗
- c语言统计各科平均分函数 c语言求科目平均分
- js将变量写入txt的简单介绍
- 马来西亚飞行模拟游戏,马来西亚的飞机
- 微信直播图片平台,微信直播图片平台下载
- vb.net遍历字体 vba遍历字符串里每个字符