一个文本文件 每行的格式为 
数字i 字母c 数字j 字符串S
现在想读入这个文本文件 然后将每行的数字 字母 数字 字符串分别分开输出
请教如何操作(不要用tio软件包)

解决方案 »

  1.   

    按你要求,给你写了一个
        /**
         * <pre>
         * [機 能] 文件读取。
         * [説 明] 按照指定路径下的文件读取,并输出
         * </pre>
         * @param strFileName 文件名
         * @param strDirectory 文件路经
         * @throws Exception 
         */
        public static void read(final String strDirectory,
                final String strFileName) throws Exception {
            File infile = null;
            BufferedReader reader = null;
            String line = null;
            try {
                infile = new File(strDirectory, strFileName);
                reader = new BufferedReader((new InputStreamReader(
                        new FileInputStream(infile))));
                while ((line = reader.readLine()) != null) { // 读取一行
                    String[] datas = line.split(" "); // 这里假设你的数据是用空格分开的
                    for (String str : datas) {
                        System.out.println(str); // 输出每个数据,数字i   字母c   数字j   字符串S...
                    }
                }
            } finally {
                try {
                    if (reader != null) {
                        reader.close();
                    }
                } catch (Exception e) {
                    throw e;
                }
            }
        }
      

  2.   

    其实就是用 Reader.readLine()读出一行后,用String.spilt()去分割.然后一个一个读出来就好了
      

  3.   

    试着写一个public final class test { public static void main(String[] args) {
    try {
    FileReader fr = new FileReader("c:/test.txt");
    Scanner scanner = new Scanner(fr);
    while (true){
    if (scanner.hasNext()){
    System.out.println(scanner.next());
    }else if (scanner.hasNextInt()){
    System.out.println(scanner.nextInt());
    }else{
    break;
    }
    }
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }
             }
    }