热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

javatxt的简单介绍

本文目录一览:1、Java中如何通过txt文件存储和取出数据?

本文目录一览:


  • 1、Java中如何通过txt文件存储和取出数据?


  • 2、java怎么从txt文件中读取数据


  • 3、java如何读取txt文件内容?


  • 4、java读txt方法


  • 5、Java中通过txt文件存储和取出数据


  • 6、Java 如何读取txt文件的内容?

Java中如何通过txt文件存储和取出数据?

Java中读取txt文件可以使用file类先创建一个对象,然后使用I/O操作,进行读取或者写入操作,示例如下:\x0d\x0aimportjava.io.BufferedReader;\x0d\x0aimportjava.io.File;\x0d\x0aimportjava.io.FileInputStream;\x0d\x0aimportjava.io.FileNotFoundException;\x0d\x0aimportjava.io.FileOutputStream;\x0d\x0aimportjava.io.IOException;\x0d\x0aimportjava.io.InputStreamReader;\x0d\x0aimportjava.io.PrintWriter;\x0d\x0a\x0d\x0apublicclassdemo2{\x0d\x0aprivatestaticStringpath="f:/demo1.txt";\x0d\x0aprivatestaticFilefile;\x0d\x0astatic{\x0d\x0afile=newFile(path);\x0d\x0aif(!file.exists()){\x0d\x0atry{\x0d\x0afile.createNewFile();\x0d\x0a}catch(IOExceptione){\x0d\x0ae.printStackTrace();\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0apublicstaticvoidmain(String[]args)throwsIOException{\x0d\x0aStudentstu=newStudent(1,"张三",90);\x0d\x0awriteDataToFile(file,stu);\x0d\x0areadDataFromFile(file);\x0d\x0a}\x0d\x0a\x0d\x0aprivatestaticvoidreadDataFromFile(Filefile)throwsIOException{\x0d\x0aBufferedReaderreader=newBufferedReader(newInputStreamReader(newFileInputStream(file)));\x0d\x0aStringstr="";\x0d\x0awhile((str=reader.readLine())!=null){\x0d\x0aString[]stuInfo=str.split(",");\x0d\x0aSystem.out.println("学号:"+stuInfo[0]+"姓名:"+stuInfo[1]+"score:"+stuInfo[2]);\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0aprivatestaticvoidwriteDataToFile(Filefile,Studentstu)throwsFileNotFoundException{\x0d\x0aPrintWriterout=newPrintWriter(newFileOutputStream(file,true));\x0d\x0aout.println(stu.toString());\x0d\x0aout.close();\x0d\x0a}\x0d\x0a}

java怎么从txt文件中读取数据

1.package txt;

2.

3.import java.io.BufferedReader;

4.import java.io.File;

5.import java.io.FileInputStream;

6.import java.io.InputStreamReader;

7.

8./**

9. * 读取TXE数据

10. */

11.public class ReadTxtUtils {

12. public static void main(String arg[]) {

13. try {

14. String encoding = "GBK"; // 字符编码(可解决中文乱码问题 )

15. File file = new File("c:/aa.txt");

16. if (file.isFile() file.exists()) {

17. InputStreamReader read = new InputStreamReader(

18. new FileInputStream(file), encoding);

19. BufferedReader bufferedReader = new BufferedReader(read);

20. String lineTXT = null;

21. while ((lineTXT = bufferedReader.readLine()) != null) {

22. System.out.println(lineTXT.toString().trim());

23. }

24. read.close();

25. }else{

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

27. }

28. } catch (Exception e) {

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

30. e.printStackTrace();

31. }

32. }

33.}

java读取TXT文件中的数据,每一行就是一个数,返回一个数组,代码?

?

List list=new ArrayList();

BufferedReader br=new BufferReader(new InputStreamReader(new FileInputStream(new File("in.txt"))));

String str=null;

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

{

list.add(new Integer(str));

}

Integer[] i=new Integer[list.size()];

list.toArray(i);

TXT文本中如据形如:

123

456

789

读入二维数组效果为:

temp[0][]={1,2,3};

temp[1][]={4,5,6};

temp[2][]={7,8,9};

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.*;

public class xx{

public static void main(String[]args){

String s;

int[][]save=new int[3][3];

try{

BufferedReader in =new BufferedReader(new FileReader("C:\\txt.txt"));

int i=0;

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

save[i][0]=Integer.parseInt(s.substring(0,1));

save[i][1]=Integer.parseInt(s.substring(1,2));

save[i][2]=Integer.parseInt(s.substring(2,3));

i++;

}

}

catch(FileNotFoundException e){

e.printStackTrace();

}

catch(IOException e){

e.printStackTrace();

}

for(int i=0;i3;i++)

{

for(int j=0;j3;j++){

System.out.print(save[i][j]);

}

System.out.println();

}

}

}

?

BufferedReader bf=new BufferedReader(new FileReader("Your file"));

String lineCOntent=null;

int i = 0;

int [][] temp = new int [3][];

while((lineCOntent=bf.readLine())!=null){

String [] str = lineContent.split("\\d");// 将 lineContent 按数字拆分

for(int j = 0; j str.length(); j++){

int [i][j] = Integer.parseInt(str[j]);

}

i++;

}

scp|cs|ff|201101

这是d:\\a.txt的数据,与“|”分割取数据出来,保存在变量a;b;c;d里

import java.io.*;

public class Test{

public static void main(String[] args)throws Exception{

String a, b, c, d;

StringBuffer sb = new StringBuffer();

BufferedReader br = new BufferedReader(new FileReader("d:\\a.txt"));

String s = br.readLine();

while(s != null){

sb.append(s);

s = br.readLine();

}

s = sb.toString();

String[] str = s.split("|");

a = str[0];

b = str[0];

c = str[0];

d = str[0];

}

}

java如何读取txt文件内容?

通常,可以直接通过文件流来读取txt文件的内容,但有时可能会出现乱码!此时只要设置一下文件字符编码即可。

(1)JAVA 读取txt文件内容

(2)读取文件效果:

java读txt方法

1).按行读取TXT文件

package zc;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

public class readLine {

public static void main(String[] args) {

// TODO Auto-generated method stub

File file = new File("C:/zc.txt");

BufferedReader reader = null;

String tempString = null;

int line =1;

try {

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

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

while ((tempString = reader.readLine()) != null) {

System.out.println("Line"+ line + ":" +tempString);

line ++ ;

}

reader.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException 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();

}

}

}

}

}

2).按字节读取TXT文件

package zc;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

public class readerFileByChars {

public static void main(String[] args) {

// TODO Auto-generated method stub

File file = new File("c:/zc.txt");

InputStream in = null;

byte[] tempByte = new byte[1024];

int byteread = 0;

try {

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

in = new FileInputStream(file);

while ((byteread = in.read(tempByte)) != -1 ) {

System.out.write(tempByte, 0, byteread);

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException 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();

}

}

}

}

}

Java中通过txt文件存储和取出数据

如果是这样的话,你就先用string的split方法以,为分隔符号分开,再replace“”,这两个东东就可以得到你要的中间的数据了。有个缺点比较占用内存,或许你也可以去读文件读到,的时候就将之前的存起来,然后再读下面的东西。思路而已试试看吧~

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”关注。


推荐阅读
author-avatar
54打工仔i_858
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有