将顺序数据存储读入列

时间:2015-10-08 07:26:21

标签: java

我有一个包含五行连续数据的文件。我希望将该文件中的字符存储到一个列中。如果我把一些固定大小的字符放入第一行四列,那么剩下的字符也将分别放入第二行四列。请帮我解决问题

我的档案是: -

Authorization is the assurance that the user is allowed to access only those resources that he is authorized to use. 
For example, in a corporate application, there are some parts of an application where only admin have access and to 
some parts all the employees have access. These access rules are determined by the access rights given to each user 
of the system. At the authorization level, spring targets three main areas: authorizing web request, authorizing 
whether methods can be invoked and authorizing access to individual domain object instances.

我的代码是

     import java.io.BufferedReader;
        import java.io.FileReader;
        import java.io.IOException;
        import java.util.ArrayList;
        import java.util.List;


       public class LoadData {
    private static String line;
    public static void main(String[] args) throws IOException {
        String col1,col2,col3,col4, col;
        List<String> str = new ArrayList<String>();
        String line;
        try {
            FileReader in = new FileReader("D:\\WebsiteDemo\\ReadingFileDemo\\data.txt");
            BufferedReader br = new BufferedReader(in);

            while ((line = br.readLine()) != null) {
                line.trim();
                str.add(line);
                System.out.println(str);
                System.out.println(line);

                int len = line.length();
                for(int i=0;i< len;i++)
                {
                    col1=line.substring(0,5);
                    col2=line.substring(5, 7);
                    col3=line.substring(7, 10);
                    col4=line.substring(10,20);
                    //line=line.substring(21, line.length());

                    System.out.println("col1...."+col1);
                    System.out.println("col2>>>...."+col2);
                    System.out.println("col3...."+col3);
                    System.out.println("col4...."+col4);
                }

            }

        } catch (FileNotFoundException e) {

            e.printStackTrace();
        }


    }
}

输出

[授权是保证允许用户仅访问他有权使用的那些资源。 ] 授权是保证允许用户仅访问他有权使用的那些资源。

col1....Autho
col2>>>....ri
col3....zat
col4....ion is the
col1....Autho
col2>>>....ri
col3....zat
col4....ion is the
col1....Autho
col2>>>....ri
col3....zat
col4....ion is the
col1....Autho
col2>>>....ri
col3....zat
col4....ion is the
col1....Autho
col2>>>....ri
col3....zat
col4....ion is the......so on until end of line

1 个答案:

答案 0 :(得分:0)

使用动态索引来获取子字符串。如下:

    int start = 0 ;
    while(start < len)
    {
        col1=line.substring(start,start+5);
        col2=line.substring(start+5, start+7);
        col3=line.substring(start+7, start+10);
        col4=line.substring(start+10,start+20);

        //line=line.substring(21, line.length());

        System.out.println("col1...."+col1);
        System.out.println("col2>>>...."+col2);
        System.out.println("col3...."+col3);
        System.out.println("col4...."+col4);
        start += 20 ;
    }

您可能必须使用字符串的长度检查子字符串参数。