读取一个txt类型的文件,文件里有很多行数据,我想把每行的数据添加到数组里,这样怎么实现?例如:txt文件的内容是:1230
456
78923String[] str={"1230","456","78923"};怎么把这些数据按行保存到数组里?

解决方案 »

  1.   

    Read this:http://blog.csdn.net/justinavril/archive/2008/08/06/2775767.aspx
      

  2.   

    使用readLine(),得到每一行,String str =- in.readLine();  然后把str给数组的每个元素
      

  3.   

    可以用楼上的方法~还有一种就是判断换行~java中有一个方法可以判断换行的~如果换行就读取到数组的下一个对象中~
      

  4.   

    好象方法海了去,用readLine()、FileReader()、DataInputStream()等等都行
      

  5.   

    File file=new File("你的文件路径");      try {
    FileReader fileread=new FileReader(file);
    BufferedReader br=new BufferedReader(fileread);
    String readStr="";
    readStr=br.readLine();
    List str=new ArrayList();
    while (readStr!=null) {
    str.add(readStr);
    readStr=br.readLine();
    }
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
      

  6.   


    使用readLine(),得到每一行,String str =- in.readLine();  然后把str给数组的每个元素
      

  7.   

    楼上的各位,你们回贴太不负责任了.楼主问的是InputStream 的问题, InputStream有readLine()方法吗?告诉你们,它只有read()方法.....import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    public class Test1 { /**
     * @param args
     */
    public static void main(String[] args) {
     try {
    InputStream is =  new FileInputStream(new File("C:\\test.txt"));
    StringBuffer sb = new StringBuffer();
    while(is.available()>0){
         int data = is.read();
         sb.append((char)data);
        }
    String[] str = sb.toString().split("\\n");
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
      

  8.   

    while(is.available()>0){ 
        int data = is.read(); 
        sb.append((char)data); 
        } 
      

  9.   

    InputStreamReader isr = new InputStreamReader(inputfile,"MS932");        
            BufferedReader buf = new BufferedReader(isr);
            String inputLine = null;
            while((inputLine=buf.readLine())!=null){
                把每一行写到你的数组里
            }
      

  10.   

    List<String> list=new ArrayList<String>();
    String str=*.readLine();
    while(str!=null)
    {
    list.add(*.readline());
    }
    String[] str=(String[])list.ToArray();
      

  11.   

    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.UnsupportedEncodingException;
    import java.util.LinkedList;
    import java.util.List;
    public class IObase { public static void main(String[] args) {
    final String CHARSET = "gb2312";
    final String PATH = "D:/IObase.java";
    String[] data = null;
    BufferedReader br = null;
    try {
    br = new BufferedReader(new InputStreamReader(new FileInputStream(PATH), CHARSET));
    List<String> list = new LinkedList<String>();
    String line = null;
    while ((line = br.readLine()) != null) {
    list.add(line);
    }
    data = list.toArray(new String[list.size()]);
    } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    if (br != null) {
    try {
    br.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    int i = 0;
    for (String s : data) {
    System.out.printf("%1$6d: %2$s\n", ++i, s);
    }
    }}
      

  12.   

    java.io.*
    使用FileReader 的readLine()方法就可以了