你如何从一个文本文件中挑一条线,并将其转换成一个数组对象? [英] How can you pick one line from a text file and transform it into an array object?

查看:84
本文介绍了你如何从一个文本文件中挑一条线,并将其转换成一个数组对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这是code和我需要以某种方式采取从文本文件一条线,转变为一个数组对象。像P [0] =asdasdasd

 公共类Patient2 {
    公共静态无效的主要(字符串ARGS [])
    {        INT场= 0;
        字符串重复=N的;
        字符串repeat1 =Y;
        扫描仪科印=新的扫描仪(System.in);        //文件I / O
        尝试{
              //打开是第一文件
              //命令行参数
              fstream的的FileInputStream =新的FileInputStream(Patient.txt);
              BR的BufferedReader =新的BufferedReader(新的InputStreamReader(fstream的));
              串strLine中;
              //读取文件一行一行
              而((strLine中= br.readLine())!= NULL){
              //打印控制台上的内容
              的System.out.println(strLine中);
              }
              //关闭输入流
              附寄();
                }赶上(例外五){//捕获异常,如果任何
              通信System.err.println(错误:+ e.getMessage());
              }
        ArrayList的< Patient1>患者=新的ArrayList< Patient1>();
        Patient1 P =新Patient1();
        //设定值到患者对象
        patients.add(P);
        的System.out.println(P);
    }
}


解决方案

只需使用的ArrayList<弦乐> 添加(strLine中); 结果,并使用的toArray(新的String [])来获取数组后输入流已经被关闭。

 的ArrayList<串GT;名单=新的ArrayList<串GT;();
 ... 而((strLine中= br.readLine())!= NULL){
    list.add(strLine中);
 }
 ... 的String [] S = list.toArray(新的String []);

Okay this is code and I need to somehow take a line from the textfile and transform into an array object. like p[0] = "asdasdasd"

public class Patient2 {
    public static void main(String args[])
    {

        int field = 0;
        String repeat = "n";
        String repeat1 = "y";
        Scanner keyIn = new Scanner(System.in);



        // FILE I/O
        try{
              // Open the file that is the first 
              // command line parameter
              FileInputStream fstream = new FileInputStream("Patient.txt");
              BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
              String strLine;
              //Read File Line By Line
              while ((strLine = br.readLine()) != null)   {
              // Print the content on the console
              System.out.println (strLine);
              }
              //Close the input stream
              in.close();
                }catch (Exception e){//Catch exception if any
              System.err.println("Error: " + e.getMessage());
              }
        ArrayList<Patient1> patients=new ArrayList<Patient1>();
        Patient1 p =new Patient1();
        //set value to the patient object
        patients.add(p);
        System.out.println(p);
    }
}

解决方案

Just use an ArrayList<String> with add(strline);
and use toArray(new String []) to get the array after input stream has been closed.

 ArrayList<String> list = new ArrayList<String>();
 ...

 while ((strLine = br.readLine()) != null) {
    list.add(strLine);
 }
 ... 

 String [] s = list.toArray(new String []);

这篇关于你如何从一个文本文件中挑一条线,并将其转换成一个数组对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆