javatxt的简单介绍

本文目录一览:

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

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/270288.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-12-16 13:36
下一篇 2024-12-16 13:36

相关推荐

  • Python简单数学计算

    本文将从多个方面介绍Python的简单数学计算,包括基础运算符、函数、库以及实际应用场景。 一、基础运算符 Python提供了基础的算术运算符,包括加(+)、减(-)、乘(*)、除…

    编程 2025-04-29
  • Python满天星代码:让编程变得更加简单

    本文将从多个方面详细阐述Python满天星代码,为大家介绍它的优点以及如何在编程中使用。无论是刚刚接触编程还是资深程序员,都能从中获得一定的收获。 一、简介 Python满天星代码…

    编程 2025-04-29
  • Python海龟代码简单画图

    本文将介绍如何使用Python的海龟库进行简单画图,并提供相关示例代码。 一、基础用法 使用Python的海龟库,我们可以控制一个小海龟在窗口中移动,并利用它的“画笔”在窗口中绘制…

    编程 2025-04-29
  • Python樱花树代码简单

    本文将对Python樱花树代码进行详细的阐述和讲解,帮助读者更好地理解该代码的实现方法。 一、简介 樱花树是一种图形效果,它的实现方法比较简单。Python中可以通过turtle这…

    编程 2025-04-28
  • Python大神作品:让编程变得更加简单

    Python作为一种高级的解释性编程语言,一直被广泛地运用于各个领域,从Web开发、游戏开发到人工智能,Python都扮演着重要的角色。Python的代码简洁明了,易于阅读和维护,…

    编程 2025-04-28
  • 用Python实现简单爬虫程序

    在当今时代,互联网上的信息量是爆炸式增长的,其中很多信息可以被利用。对于数据分析、数据挖掘或者其他一些需要大量数据的任务,我们可以使用爬虫技术从各个网站获取需要的信息。而Pytho…

    编程 2025-04-28
  • 如何制作一个简单的换装游戏

    本文将从以下几个方面,为大家介绍如何制作一个简单的换装游戏: 1. 游戏需求和界面设计 2. 使用HTML、CSS和JavaScript开发游戏 3. 实现游戏的基本功能:拖拽交互…

    编程 2025-04-27
  • Guava Limiter——限流器的简单易用

    本文将从多个维度对Guava Limiter进行详细阐述,介绍其定义、使用方法、工作原理和案例应用等方面,并给出完整的代码示例,希望能够帮助读者更好地了解和使用该库。 一、定义 G…

    编程 2025-04-27
  • 制作一个简单的管理系统的成本及实现

    想要制作一个简单的管理系统,需要进行技术选型、开发、测试等过程,那么这个过程会花费多少钱呢?我们将从多个方面来阐述制作一个简单的管理系统的成本及实现。 一、技术选型 当我们开始思考…

    编程 2025-04-27
  • 2的32次方-1:一个看似简单却又复杂的数字

    对于计算机领域的人来说,2的32次方-1(也就是十进制下的4294967295)这个数字并不陌生。它经常被用来表示IPv4地址或者无符号32位整数的最大值。但实际上,这个数字却包含…

    编程 2025-04-27

发表回复

登录后才能评论