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/zh-tw/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

發表回復

登錄後才能評論