资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java读txt文件代码的简单介绍

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 = new String[readLines()];int i;for(i= ; i readLines() 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 如何读取txt文件的内容?

java读取txt文件内容。可以作如下理解:

首先获得一个文件句柄。File file = new File(); file即为文件句柄。两人之间连通电话网络了。接下来可以开始打电话了。

通过这条线路读取甲方的信息:new FileInputStream(file) 目前这个信息已经读进来内存当中了。接下来需要解读成乙方可以理解的东西

既然你使用了FileInputStream()。那么对应的需要使用InputStreamReader()这个方法进行解读刚才装进来内存当中的数据

解读完成后要输出呀。那当然要转换成IO可以识别的数据呀。那就需要调用字节码读取的方法BufferedReader()。同时使用bufferedReader()的readline()方法读取txt文件中的每一行数据哈。

package com.campu;

import java.io.BufferedInputStream;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStreamReader;

import java.io.Reader;

/**

* @author Java团长

* H20121012.java

* 2017-10-29上午11:22:21

*/

public class H20121012 {

/**

* 功能:Java读取txt文件的内容

* 步骤:1:先获得文件句柄

* 2:获得文件句柄当做是输入一个字节码流,需要对这个输入流进行读取

* 3:读取到输入流后,需要读取生成字节流

* 4:一行一行的输出。readline()。

* 备注:需要考虑的是异常情况

* @param filePath

*/

public static void readTxtFile(String filePath){

try {

String encoding="GBK";

File file=new File(filePath);

if(file.isFile()  file.exists()){ //判断文件是否存在

InputStreamReader read = new InputStreamReader(

new FileInputStream(file),encoding);//考虑到编码格式

BufferedReader bufferedReader = new BufferedReader(read);

String lineTxt = null;

while((lineTxt = bufferedReader.readLine()) != null){

System.out.println(lineTxt);

}

read.close();

}else{

System.out.println("找不到指定的文件");

}

} catch (Exception e) {

System.out.println("读取文件内容出错");

e.printStackTrace();

}

}

public static void main(String argv[]){

String filePath = "L:\\Apache\\htdocs\\res\\20121012.txt";

//      "res/";

readTxtFile(filePath);

}

}

我有一个微信公众号,经常会分享一些Java技术相关的干货文章,还有一些学习资源。

如果你需要的话,可以用微信搜索“Java团长”或者“javatuanzhang”关注。

怎样用JAVA打开一个已经写好的TXT文件?

JAVA打开一个已经写好的TXT文件代码如下:

(1):public File chooseFile(int chooseMode)

{

JFileChooser fileChooser;

File fileName;

fileChooser=new JFileChooser();

int returnVal=-1;

// fileFilter=new FileNameExtensionFilter("文本文档","txt");

switch(chooseMode)

{

case FILE_OPEN_CHOOSE:

returnVal=fileChooser.showOpenDialog(this);

break;

case FILE_SAVE_CHOOSE:

returnVal=fileChooser.showSaveDialog(this);

break;

}

if(returnVal==fileChooser.APPROVE_OPTION)

fileName=fileChooser.getSelectedFile();

else fileName=null;

return fileName;

}

这个是通过打开文件对话框获取文件

public void showFile(File file) throws IOException

{

textArea.setText("");

BufferedReader br=new BufferedReader(new FileReader(file));

String text;

// buffer=new StringBuffer();

while((text=br.readLine())!=null)

// buffer.append(text+"\n");

textArea.append(text+"\n");

}

(2):import java.io.*;

import java.util.*;

public class readData

{

public static void main(String[] args)

{

BufferedReader in=new BufferedReader(new FileReader("你的文件名.txt"));

String s;

int i=0;

while((s=in.readLine())!=null)

{

StringTokenizer t=new StringTokenizer(s,"|");//"|"为分隔符

p[i].id=Integer.parseInt(t.nextToken());//将第一个记录赋给变量id

p[i].name=t.nextToken();//将第二个记录赋给变量name

p[i].age=Integer.parseInt(t.nextToken());//将第三个记录赋给变量age

p[i].sex=t.nextToken();//将第四个记录赋给变量sex

i++;

}

in.close();

}

}

//**********************************************************

假设赋值得变量结构如下:

class p

{

public int id;

public String name;

public int age;

public String sex;

}


网页名称:java读txt文件代码的简单介绍
文章位置:http://cdkjz.cn/article/ddgscii.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220