JAVA中,文件的输入输出(1)

本文介绍如何从文件中读取数据并将其转换为一维数组,包括数组元素的类型转换和基本操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一,从文件中读取数据

package com.File1.java;

//import java.util.*;
import java.io.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ReadFile1 {
    public static void main(String [] args) throws IOException
    {
        try
        {
        File f=new File("E:/test.txt");
        //BufferedReader bre=new BufferedReader(new FileReader(f));
        BufferedReader bre =new BufferedReader(new InputStreamReader(new FileInputStream(f)));
        String str=null;
        while((str=bre.readLine())!=null)
        {
            System.out.println(str);
        }
        bre.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

}

2,从文件中读取一维数组,在txt文件中先建立例如1 2 3 4 5 6 7.

package com.File1.java;

//import java.util.*;
import java.io.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ReadFile1 {

    private static int N=100;
    public static void main(String [] args) throws IOException
    {
        try
        {

        double []shuzu=new double[N];   
        int [] shuzu2=new int[N];
        String [] shuzu3=new String[N];
        File f=new File("E:/test.txt");
        //@SuppressWarnings("resource")
        //BufferedReader bre=new BufferedReader(new FileReader(f));
        //BufferedReader bre =new BufferedReader(new InputStreamReader(new FileInputStream(f)));
        BufferedReader bre =new BufferedReader(new FileReader(f));

        String line;
        int k=0;
        while((line=bre.readLine())!=null)
        {
            String []temp=line.split(" ");

            for(int j=0;j<temp.length;j++)
            {
                shuzu[j]=Double.parseDouble(temp[j]);
                shuzu2[j]=Integer.parseInt(temp[j]);
                shuzu3[j]=temp[j];
                //System.out.print(shuzu[j]+" ");
                k++;
            }
        }
        /*
        String line2;
        while((line2=bre.readLine())!=null)
        {
            String [] temp1=line2.split(" ");  //就是读取字符串一直读取到空格位置
            for(int j=0;j<temp1.length;j++)
            {
                shuzu2[j]=Integer.valueOf(temp1[j]);//parseInt(temp1[j]);
            }
        }
        */
        System.out.print("转化成double型: ");

        for(int i=0;i<k;i++)
        {
            System.out.print(shuzu[i]+"  ");
        }
        System.out.println();
        System.out.print("转化成int型: ");
        for(int m=0;m<k;m++)
        {
            System.out.print(shuzu2[m]+" ");
        }
        System.out.println();
        System.out.print("转换成String型: ");
        for(int n=0;n<k;n++)
        {
            System.out.print(shuzu3[n]+" ");

        }
        System.out.println();


        //System.out.println(bre);
        /*String s="";
        String s1;
        while((s1=bre.readLine())!=null)
        {
            s=s+s1;
        }

        String[] str=s.split("");
        System.out.println(str);
        int []datas=new int[str.length];

        for(int j=0;j<str.length;j++)
        {
            datas[j]=Integer.parseInt(str[j]);
        }

        for(int i=0;i<datas.length;i++)
        {
            System.out.print(datas[i]+" ");
        }

        bre.close();

        System.out.println();

        */
        /*
        String str=null;
        while((str=bre.readLine())!=null)
        {
            System.out.println(str);
        }
        bre.close();

        */

        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值