android 读取txt文件获取每行内容

android 读取txt文件获取每行内容,第1张

方法:

通过输入流的readline方法进行按行读取内容

举例:

public static void readFileOnLine(String filePath){//输入文件路径

   FileInputStream fis = openFileInput(filePath);//打开文件输入流

   StringBuffer sBuffer = new StringBuffer();

   DataInputStream dataIO = new DataInputStream(fis);//读取文件数据流

   String strLine = null;

   while((strLine =  dataIOreadLine()) != null) {//通过readline按行读取

      sBufferappend(strLine + "\n");//strLine就是一行的内容

   }

  dataIOclose();

  fisclose();

}

JAVA中读取文件内容的方法有很多,比如按字节读取文件内容,按字符读取文件内容,按行读取文件内容,随机读取文件内容等方法,本文就以上方法的具体实现给出代码,需要的可以直接复制使用

public class ReadFromFile {

/

以字节为单位读取文件,常用于读二进制文件,如、声音、影像等文件。

/

public static void readFileByBytes(String fileName) {

File file = new File(fileName);

InputStream in = null;

try {

Systemoutprintln("以字节为单位读取文件内容,一次读一个字节:");

// 一次读一个字节

in = new FileInputStream(file);

int tempbyte;

while ((tempbyte = inread()) != -1) {

Systemoutwrite(tempbyte);

}

inclose();

} catch (IOException e) {

eprintStackTrace();

return;

}

try {

Systemoutprintln("以字节为单位读取文件内容,一次读多个字节:");

// 一次读多个字节

byte[] tempbytes = new byte[100];

int byteread = 0;

in = new FileInputStream(fileName);

ReadFromFileshowAvailableBytes(in);

// 读入多个字节到字节数组中,byteread为一次读入的字节数

while ((byteread = inread(tempbytes)) != -1) {

Systemoutwrite(tempbytes, 0, byteread);

}

} catch (Exception e1) {

e1printStackTrace();

} finally {

if (in != null) {

try {

inclose();

} catch (IOException e1) {

}

}

}

}

/

以字符为单位读取文件,常用于读文本,数字等类型的文件

/

public static void readFileByChars(String fileName) {

File file = new File(fileName);

Reader reader = null;

try {

Systemoutprintln("以字符为单位读取文件内容,一次读一个字节:");

// 一次读一个字符

reader = new InputStreamReader(new FileInputStream(file));

int tempchar;

while ((tempchar = readerread()) != -1) {

// 对于windows下,\r\n这两个字符在一起时,表示一个换行。

// 但如果这两个字符分开显示时,会换两次行。

// 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。

if (((char) tempchar) != '\r') {

Systemoutprint((char) tempchar);

}

}

readerclose();

} catch (Exception e) {

eprintStackTrace();

}

try {

Systemoutprintln("以字符为单位读取文件内容,一次读多个字节:");

// 一次读多个字符

char[] tempchars = new char[30];

int charread = 0;

reader = new InputStreamReader(new FileInputStream(fileName));

// 读入多个字符到字符数组中,charread为一次读取字符数

while ((charread = readerread(tempchars)) != -1) {

// 同样屏蔽掉\r不显示

if ((charread == tempcharslength)

&& (tempchars[tempcharslength - 1] != '\r')) {

Systemoutprint(tempchars);

} else {

for (int i = 0; i < charread; i++) {

if (tempchars[i] == '\r') {

continue;

} else {

Systemoutprint(tempchars[i]);

}

}

}

}

} catch (Exception e1) {

e1printStackTrace();

} finally {

if (reader != null) {

try {

readerclose();

} catch (IOException e1) {

}

}

}

}

/

以行为单位读取文件,常用于读面向行的格式化文件

/

public static void readFileByLines(String fileName) {

File file = new File(fileName);

BufferedReader reader = null;

try {

Systemoutprintln("以行为单位读取文件内容,一次读一整行:");

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

String tempString = null;

int line = 1;

// 一次读入一行,直到读入null为文件结束

while ((tempString = readerreadLine()) != null) {

// 显示行号

Systemoutprintln("line " + line + ": " + tempString);

line++;

}

readerclose();

} catch (IOException e) {

eprintStackTrace();

} finally {

if (reader != null) {

try {

readerclose();

} catch (IOException e1) {

}

}

}

}

/

随机读取文件内容

/

public static void readFileByRandomAccess(String fileName) {

RandomAccessFile randomFile = null;

try {

Systemoutprintln("随机读取一段文件内容:");

// 打开一个随机访问文件流,按只读方式

randomFile = new RandomAccessFile(fileName, "r");

// 文件长度,字节数

long fileLength = randomFilelength();

// 读文件的起始位置

int beginIndex = (fileLength > 4) 4 : 0;

// 将读文件的开始位置移到beginIndex位置。

randomFileseek(beginIndex);

byte[] bytes = new byte[10];

int byteread = 0;

// 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。

// 将一次读取的字节数赋给byteread

while ((byteread = randomFileread(bytes)) != -1) {

Systemoutwrite(bytes, 0, byteread);

}

} catch (IOException e) {

eprintStackTrace();

} finally {

if (randomFile != null) {

try {

randomFileclose();

} catch (IOException e1) {

}

}

}

}

/

显示输入流中还剩的字节数

/

private static void showAvailableBytes(InputStream in) {

try {

Systemoutprintln("当前字节输入流中的字节数为:" + inavailable());

} catch (IOException e) {

eprintStackTrace();

}

}

public static void main(String[] args) {

String fileName = "C:/temp/newTemptxt";

ReadFromFilereadFileByBytes(fileName);

ReadFromFilereadFileByChars(fileName);

ReadFromFilereadFileByLines(fileName);

ReadFromFilereadFileByRandomAccess(fileName);

}

}

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

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

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

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

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

package comcampu;

 

import javaioBufferedInputStream;

import javaioBufferedReader;

import javaioFile;

import javaioFileInputStream;

import javaioInputStreamReader;

import javaioReader;

 

/

  @author Java团长

  H20121012java

  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(fileisFile() && fileexists()){ //判断文件是否存在

                    InputStreamReader read = new InputStreamReader(

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

                    BufferedReader bufferedReader = new BufferedReader(read);

                    String lineTxt = null;

                    while((lineTxt = bufferedReaderreadLine()) != null){

                        Systemoutprintln(lineTxt);

                    }

                    readclose();

        }else{

            Systemoutprintln("找不到指定的文件");

        }

        } catch (Exception e) {

            Systemoutprintln("读取文件内容出错");

            eprintStackTrace();

        }

     

    }

     

    public static void main(String argv[]){

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

//      "res/";

        readTxtFile(filePath);

    }

     

     

 

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

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

#include <stdioh>

#include <stdlibh>

#include <stringh>

#define BUF_SIZE 1024

#define KEY "AB2345"

#define KEY_LEN 7

int main()

{

int ch = 0;

int first = 1;//开始时的标志,因为是一个字符一个字符的扫描

int flag = 0;//文件开头是不是所要读内容的标志

int count = 0;//遇到'\n'的个数

int pre_pos = 0, cur_pos = 0;//前一次和当前文件指针的位置

char buf[BUF_SIZE] = {0};

FILE fp = NULL;

fp = fopen("testtxt", "r");

if (fp == NULL)

{

printf("Cann't open the file!\n");

exit(1);

}

else

{

while ((ch = fgetc(fp)) != EOF)

{

if (first)

{

//若要读取的内容在文件开头就有时

//移动指针到文件开头

fseek(fp, -1L, SEEK_CUR);

fgets(buf, KEY_LEN, fp);

if (strcmp(buf, KEY) == 0)

{

first = 0;

flag = 1;

continue;

}

else

{

first = 0;

}

}

if (ch == '\n')

{

count++;//遇到'\n'的个数

pre_pos = cur_pos;//上次遇到'\n'时文件指针的位置

cur_pos = ftell(fp);//当前遇到'\n'时文件指针的位置

//文件开头内容符合要求的就适当移动指针位置

//然后读取输出来

if (count == 1 && flag == 1)

{

fseek(fp, 0L, SEEK_SET);

memset(buf, 0, sizeof(buf));

fgets(buf, cur_pos - 1, fp);

printf("%s\n", buf);

}

//之后内容符合要求的就适当移动指针位置

//然后读取输出来

else

{

memset(buf, 0, sizeof(buf));

fgets(buf, KEY_LEN, fp);

if (strcmp(buf, KEY) == 0)

{

fseek(fp, (-1) (KEY_LEN - 1), SEEK_CUR);

memset(buf, 0, sizeof(buf));

fgets(buf, cur_pos-1-pre_pos, fp);

printf("%s\n", buf);

}

}

}

}

}

fclose(fp);

return 0;

}

<php

//取得指定位址的内容,并储存至 $text

$text=file_get_contents('>

以上就是关于android 读取txt文件获取每行内容全部的内容,包括:android 读取txt文件获取每行内容、JAVA中读取文件(二进制,字符)内容的几种方、Java 如何读取txt文件的内容等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9269759.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-26
下一篇2023-04-26

发表评论

登录后才能评论

评论列表(0条)

    保存