Android:如何使用多行长的项目拆分非常长的String?

时间:2014-07-22 01:50:18

标签: java android string arraylist split

我有一个非常长的字符串,它来自我的应用从互联网下载的文件。现在,在我将文件转换为字符串后,我使用String.split拆分字符串,然后通过文件将我已经拆分的字符串(字符串)拆分为字符串"描述"通常包含多行。之后我将这些字符串放在一个自定义的arraylist中,然后我使用一个自定义适配器来显示我放在arraylist中的每个项目,除了它只显示第一行描述。如何制作它以显示"描述中的所有行?

如何制作,以便read.line考虑"描述"碎片作为一条线,或者是否有某种方式使它成为"描述"总是合并在一起?

这是我尝试解析的字符串的内容:

"","ZQ11ISF1","1","INSTRUCTIONAL SKILLS","MARKOVIC","","","","" "","TQS11QP2","1","COMPUTER PROGRAMMING 2","THORP","","","","" "","ZLF","1","FRIDAY LUNCH","MARKOVIC","","","","" "","PHS84","17","HEALTH & WELLNESS TERM 4 OF 8","REED","","","","" "","PQS11QPI","1","INDV. PHYS ED","MARKOVIC","","","","" "18342","SCS22","1","CHEMISTRY 2 OF 2","Mott","Chemistry HW for tomorrow","2014-06-03","Homework","

Hello chemistry students,



We did the acid base conductivity lab today in class.  There are 29 questions and 11 summary questions.  I stated at the beginning of class today that there would be no homework except for determining who your partner(s) will be for the PBA presentation.  I’ll stand by that.  In class tomorrow we will finish the questions.  I hope you’ve started to research/write your PBA paper.  Remember to write down the information (title, author, web address, webpage title, date of publication if possible, publisher and location if book)  of any source you use so that you can put it into you bibliography at the end.  Your textbook would be a wise choice as one of your sources.


Note:

The Bronsted-Lowry Theory (1932) of Acids and Bases (which we have been adopting in class so far) states:
Acids donate protons (H+)

Bases accept protons (H+)

The Arrhenius Theory (1887) of Acids and Bases (which this lab introduces on the back of the second page) states:

Acids produce protons (H+)

Bases produce hydroxide ions (OH-)

Historically, the Bronsted-Lowry Theory came second and it doesn’t contradict the Arrhenius Theory. It only broadens it because the Arrhenius Theory limits bases to chemicals with OH- ions.  Some chemicals, however, like NH3 (which don’t have OH- ions) do act like bases by neutralizing acids.   Neutralize means that an acid and a base combine to form a salt, sometimes water would need to be evaporated to extract the salt.

Neutrally,

Mr. Mott

" "","SXGPW","1","PBAT Science","","","","","" "","MXGRW","1","PBAT Algebra 2/Trig ","","","","","" "","EES84","4","ENGLISH 10 2 OF 2","GURALNICK","","","","" "18265","FSS84","8","SPANISH II 2 OF 2","RATCLIFFE","Tarea para 6/3","2014-06-03","Homework","

 1. The last page of your PBA review packets both readings.

ANSWER QUESTIONS IN COMPLETE SENTENCES.

" "18413","HGS44","10","GLOBAL STUDIES 10 2 OF 2","Anderson","Intro Paragraph and EDAC","2014-06-03","Homework","

 For homework tonight, please finish your introductory paragraph and any remaining EDAC sheets for your essay. Come to class tomorrow with a printed copy (in print, no phones!!!) of your intro and EDAC sheets finished for each of your body paragraphs.

Have a great night!

" "18330","MRS22","7","ALGEBRA 2 / TRIG 2 OF 2","KLEIN","HW #28","2014-06-03","Homework","

 A.  pg. 803 #24,25,28,30,31

B.  Check your answers here.

www.beaconschool.org/~sklein/Algebra2/a2hwtriglos.pdf

C.  Create the FIRST half of your Study Sheet for the Trig Test on Monday, 6/9/14. 

"

编辑:我已经发现read.line实际上每次读取一个新行时都会再次启动while循环,但它会记住它所在的变量,因此它会创建一个全新的"描述&# 34;每次遇到一个换行符号时(新行只在"描述"字符串的一部分)并且被添加到自定义的arraylist中。因此,我需要弄清楚如何制作它,以便它不会认为描述被分成不同的行。

编辑:我认为使用扫描仪会解决我的问题,但是如果我在while循环中使用扫描仪,它不会在每次循环停止的地方继续,并且它不会自行关闭当它完成文件时。

我试过的事情:

  • 制作字符串"说明"所有一行(然后实际的解析器不返回任何内容)
  • 将.trim()添加到最后(什么都不做)
  • 删除"检查是否"输入"是null",它添加了许多ListView部分,其中包含String的其他行"描述"

以下是实际解析器的代码

Log.d("homework due today", Due_Today);


            InputStream is = new ByteArrayInputStream(Due_Today.getBytes());


            BufferedReader reader = new BufferedReader(new InputStreamReader(is));



            try {

                while ((Due_Today = reader.readLine()) != null) {   
            String[] part = Due_Today.split("\",\"", -1);
            int noOfItems = part.length;
            int counter=0;
            Description = counter<noOfItems ? part[counter] : "";
            counter++;
            Band = counter<noOfItems ? part[counter] : "";
            counter++;
            Number = counter<noOfItems ? part[counter] : "";
            counter++;
            Class = counter<noOfItems ? part[counter] : "";
            counter++;
            Teacher = counter<noOfItems ? part[counter] : "";
            counter++;
            Title = counter<noOfItems ? part[counter] : "";
            counter++;
            Date = counter<noOfItems ? part[counter] : "";
            counter++;
            Type = counter<noOfItems ? part[counter] : "";
            counter++;
            Data = counter<noOfItems ? part[counter] : "";
            counter++; 


            Description = Description.replaceAll("^\"|\"$", "");
            Band = Band.replaceAll("^\"|\"$", "");
            Number = Number.replaceAll("^\"|\"$", "");
            Class = Class.replaceAll("^\"|\"$", "");
            Teacher = Teacher.replaceAll("^\"|\"$", "");
            Title = Title.replaceAll("^\"|\"$", "");
            Date = Date.replaceAll("^\"|\"$", "");
            Type = Type.replaceAll("^\"|\"$", "");
            Data = Data.replaceAll("^\"|\"$", "");






            Log.d("Data", Data);
            Log.d("Band", Band);
            Log.d("Number", Number);
            Log.d("Class", Class);
            Log.d("Teacher", Teacher);
            Log.d("Title", Title);
            Log.d("Date", Date);
            Log.d("Type", Type);
            Log.d("Description", Description);



            due_today_list.add(new Due_Today_List(Data, Band, Number, Class, Teacher, Title, Date, Type, Description));

自定义适配器代码

private void populateListView() {
        ArrayAdapter<Due_Today_List> adapter = new Due_TodayAdapter();
        ListView list = (ListView)getView().findViewById(R.id.listView1);
        list.setAdapter(adapter);




    }


    public class Due_TodayAdapter extends ArrayAdapter<Due_Today_List>  {
        public Due_TodayAdapter() {
            super(getActivity(), R.layout.item_view, due_today_list);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;




            if(convertView==null){
            convertView = getActivity().getLayoutInflater().inflate(R.layout.item_view, parent, false);
             holder = new ViewHolder();



             holder.imageView = (ImageView)convertView.findViewById(R.id.item_iconclass);



             holder.HomeworkDueText = (TextView) convertView.findViewById(R.id.item_texthomeworkdue);

            holder.DescriptionText = (TextView) convertView.findViewById(R.id.item_textdescription);

            holder.TeacherText = (TextView) convertView.findViewById(R.id.item_textteacher);

            holder.TypeText = (TextView) convertView.findViewById(R.id.item_texttype);

            convertView.setTag(holder);


            }

        else {
            holder = (ViewHolder) convertView.getTag();
        }



        Due_Today_List currenthomeworkdue = due_today_list.get(position);

            if(currenthomeworkdue.getType() != null && !currenthomeworkdue.getType().isEmpty()){






        Teacher = currenthomeworkdue.getTeacher().substring(0,1).toUpperCase()+currenthomeworkdue.getTeacher().substring(1).toLowerCase();

        Description = currenthomeworkdue.getDescription().substring(5); 



        if(currenthomeworkdue.Band.substring(0, Math.min(currenthomeworkdue.Band.length(), 1)).equals("S")){

        holder.imageView.setImageResource(R.drawable.science);


        }


        if(currenthomeworkdue.Band.substring(0, Math.min(currenthomeworkdue.Band.length(), 1)).equals("M")){

            holder.imageView.setImageResource(R.drawable.mathematics);


            }


        if(currenthomeworkdue.Band.substring(0, Math.min(currenthomeworkdue.Band.length(), 1)).equals("H")){

            holder.imageView.setImageResource(R.drawable.global_studies);


            }

        if(currenthomeworkdue.Band.substring(0, Math.min(currenthomeworkdue.Band.length(), 2)).equals("FS")){

            holder.imageView.setImageResource(R.drawable.spanish);


            }

        if(currenthomeworkdue.Band.substring(0, Math.min(currenthomeworkdue.Band.length(), 1)).equals("E")){

            holder.imageView.setImageResource(R.drawable.english);


            }

        holder.HomeworkDueText.setText(currenthomeworkdue.getTitle());

        holder.DescriptionText.setText(Description);


        holder.TeacherText.setText(Teacher);

        holder.TypeText.setText(currenthomeworkdue.getType());

        return convertView;

            } else {


                View empty = getActivity().getLayoutInflater().inflate(R.layout.empty_item, parent, false);



       return empty;






        }

        }

以下是Log返回的内容

07-21 21:33:14.704: D/homework due today(8127): "","ZQ11ISF1","1","INSTRUCTIONAL SKILLS","MARKOVIC","","","",""
07-21 21:33:14.704: D/homework due today(8127): "","TQS11QP2","1","COMPUTER PROGRAMMING 2","THORP","","","",""
07-21 21:33:14.704: D/homework due today(8127): "","ZLF","1","FRIDAY LUNCH","MARKOVIC","","","",""
07-21 21:33:14.704: D/homework due today(8127): "","PHS84","17","HEALTH & WELLNESS TERM 4 OF 8","REED","","","",""
07-21 21:33:14.704: D/homework due today(8127): "","PQS11QPI","1","INDV. PHYS ED","MARKOVIC","","","",""
07-21 21:33:14.704: D/homework due today(8127): "18342","SCS22","1","CHEMISTRY 2 OF 2","Mott","Chemistry HW for tomorrow","2014-06-03","Homework","
07-21 21:33:14.704: D/homework due today(8127):  
07-21 21:33:14.704: D/homework due today(8127):  
07-21 21:33:14.704: D/homework due today(8127): Hello chemistry students,
07-21 21:33:14.704: D/homework due today(8127): We did the acid base conductivity lab today in class.  There are 29 questions and 11 summary questions.  I stated at the beginning of class today that there would be no homework except for determining who your partner(s) will be for the PBA presentation.  I’ll stand by that.  In class tomorrow we will finish the questions.  I hope you’ve started to research/write your PBA paper.  Remember to write down the information (title, author, web address, webpage title, date of publication if possible, publisher and location if book)  of any source you use so that you can put it into you bibliography at the end.  Your textbook would be a wise choice as one of your sources.
07-21 21:33:14.704: D/homework due today(8127): Note:
07-21 21:33:14.704: D/homework due today(8127): The Bronsted-Lowry Theory (1932) of Acids and Bases (which we have been adopting in class so far) states:?  
07-21 21:33:14.704: D/homework due today(8127): Acids donate protons (H+)
07-21 21:33:14.704: D/homework due today(8127): Bases accept protons (H+)
07-21 21:33:14.704: D/homework due today(8127): The Arrhenius Theory (1887) of Acids and Bases (which this lab introduces on the back of the second page) states: 
07-21 21:33:14.704: D/homework due today(8127): Acids produce protons (H+)
07-21 21:33:14.704: D/homework due today(8127): Bases produce hydroxide ions (OH-)
07-21 21:33:14.704: D/homework due today(8127): Historically, the Bronsted-Lowry Theory came second and it doesn’t contradict the Arrhenius Theory. It only broadens it because the Arrhenius Theory limits bases to chemicals with OH- ions.  Some chemicals, however, like NH3 (which don’t have OH- ions) do act like bases by neutralizing acids.   Neutralize means that an acid and a base combine to form a salt, sometimes water would need to be evaporated to extract the salt. 
07-21 21:33:14.704: D/homework due today(8127):  
07-21 21:33:14.704: D/homework due today(8127): Neutrally,
07-21 21:33:14.704: D/homework due today(8127): Mr. Mott
07-21 21:33:14.704: D/homework due today(8127):  
07-21 21:33:14.704: D/homework due today(8127): "
07-21 21:33:14.704: D/homework due today(8127): "","SXGPW","1","PBAT Science","","","","",""
07-21 21:33:14.704: D/homework due today(8127): "","MXGRW","1","PBAT Algebra 2/Trig ","","","","",""
07-21 21:33:14.704: D/homework due today(8127): "","EES84","4","ENGLISH 10 2 OF 2","GURALNICK","","","",""
07-21 21:33:14.704: D/homework due today(8127): "18265","FSS84","8","SPANISH II 2 OF 2","RATCLIFFE","Tarea para 6/3","2014-06-03","Homework","
07-21 21:33:14.704: D/homework due today(8127):  1. The last page of your PBA review packets both readings.
07-21 21:33:14.704: D/homework due today(8127):  
07-21 21:33:14.704: D/homework due today(8127): ANSWER QUESTIONS IN COMPLETE SENTENCES.
07-21 21:33:14.704: D/homework due today(8127): "
07-21 21:33:14.704: D/homework due today(8127): "18413","HGS44","10","GLOBAL STUDIES 10 2 OF 2","Anderson","Intro Paragraph and EDAC","2014-06-03","Homework","
07-21 21:33:14.704: D/homework due today(8127):  For homework tonight, please finish your introductory paragraph and any remaining EDAC sheets for your essay. Come to class tomorrow with a printed copy (in print, no phones!!!) of your intro and EDAC sheets finished for each of your body paragraphs.
07-21 21:33:14.704: D/homework due today(8127): Have a great night!
07-21 21:33:14.704: D/homework due today(8127): "
07-21 21:33:14.704: D/homework due today(8127): "18330","MRS22","7","ALGEBRA 2 / TRIG 2 OF 2","KLEIN","HW #28","2014-06-03","Homework","
07-21 21:33:14.704: D/homework due today(8127):  A.  pg. 803 #24,25,28,30,31
07-21 21:33:14.704: D/homework due today(8127): B.  Check your answers here.
07-21 21:33:14.704: D/homework due today(8127): www.beaconschool.org/~sklein/Algebra2/a2hwtriglos.pdf
07-21 21:33:14.704: D/homework due today(8127): C.  Create the FIRST half of your Study Sheet for the Trig Test on Monday, 6/9/14. 
07-21 21:33:14.704: D/homework due today(8127): "
07-21 21:33:14.704: D/homework due today(8127):     
07-21 21:33:14.714: D/Band(8127): ZQ11ISF1
07-21 21:33:14.714: D/Number(8127): 1
07-21 21:33:14.714: D/Class(8127): INSTRUCTIONAL SKILLS
07-21 21:33:14.714: D/Teacher(8127): MARKOVIC
07-21 21:33:14.714: D/Band(8127): TQS11QP2
07-21 21:33:14.714: D/Number(8127): 1
07-21 21:33:14.714: D/Class(8127): COMPUTER PROGRAMMING 2
07-21 21:33:14.714: D/Teacher(8127): THORP
07-21 21:33:14.714: D/Band(8127): ZLF
07-21 21:33:14.714: D/Number(8127): 1
07-21 21:33:14.714: D/Class(8127): FRIDAY LUNCH
07-21 21:33:14.714: D/Teacher(8127): MARKOVIC
07-21 21:33:14.714: D/Band(8127): PHS84
07-21 21:33:14.724: D/Number(8127): 17
07-21 21:33:14.724: D/Class(8127): HEALTH & WELLNESS TERM 4 OF 8
07-21 21:33:14.724: D/Teacher(8127): REED
07-21 21:33:14.724: D/Band(8127): PQS11QPI
07-21 21:33:14.724: D/Number(8127): 1
07-21 21:33:14.724: D/Class(8127): INDV. PHYS ED
07-21 21:33:14.724: D/Teacher(8127): MARKOVIC
07-21 21:33:14.724: D/Band(8127): SCS22
07-21 21:33:14.724: D/Number(8127): 1
07-21 21:33:14.724: D/Class(8127): CHEMISTRY 2 OF 2
07-21 21:33:14.734: D/Teacher(8127): Mott
07-21 21:33:14.734: D/Title(8127): Chemistry HW for tomorrow
07-21 21:33:14.734: D/Date(8127): 2014-06-03
07-21 21:33:14.734: D/Type(8127): Homework
07-21 21:33:14.734: D/Description(8127): 18342
07-21 21:33:14.734: D/Description(8127):  
07-21 21:33:14.744: D/Description(8127):  
07-21 21:33:14.754: D/Description(8127): Hello chemistry students,
07-21 21:33:14.754: D/Description(8127): We did the acid base conductivity lab today in class.  There are 29 questions and 11 summary questions.  I stated at the beginning of class today that there would be no homework except for determining who your partner(s) will be for the PBA presentation.  I’ll stand by that.  In class tomorrow we will finish the questions.  I hope you’ve started to research/write your PBA paper.  Remember to write down the information (title, author, web address, webpage title, date of publication if possible, publisher and location if book)  of any source you use so that you can put it into you bibliography at the end.  Your textbook would be a wise choice as one of your sources.
07-21 21:33:14.764: D/Description(8127): Note:
07-21 21:33:14.774: D/Description(8127): The Bronsted-Lowry Theory (1932) of Acids and Bases (which we have been adopting in class so far) states:?  
07-21 21:33:14.774: D/Description(8127): Acids donate protons (H+)
07-21 21:33:14.784: D/Description(8127): Bases accept protons (H+)
07-21 21:33:14.784: D/Description(8127): The Arrhenius Theory (1887) of Acids and Bases (which this lab introduces on the back of the second page) states: 
07-21 21:33:14.794: D/Description(8127): Acids produce protons (H+)
07-21 21:33:14.804: D/Description(8127): Bases produce hydroxide ions (OH-)
07-21 21:33:14.804: D/Description(8127): Historically, the Bronsted-Lowry Theory came second and it doesn’t contradict the Arrhenius Theory. It only broadens it because the Arrhenius Theory limits bases to chemicals with OH- ions.  Some chemicals, however, like NH3 (which don’t have OH- ions) do act like bases by neutralizing acids.   Neutralize means that an acid and a base combine to form a salt, sometimes water would need to be evaporated to extract the salt. 
07-21 21:33:14.804: D/Description(8127):  
07-21 21:33:14.814: D/Description(8127): Neutrally,
07-21 21:33:14.814: D/Description(8127): Mr. Mott
07-21 21:33:14.824: D/Description(8127):  
07-21 21:33:14.824: D/Band(8127): SXGPW
07-21 21:33:14.824: D/Number(8127): 1
07-21 21:33:14.824: D/Class(8127): PBAT Science
07-21 21:33:14.824: D/Band(8127): MXGRW
07-21 21:33:14.824: D/Number(8127): 1
07-21 21:33:14.824: D/Class(8127): PBAT Algebra 2/Trig 
07-21 21:33:14.824: D/Band(8127): EES84
07-21 21:33:14.824: D/Number(8127): 4
07-21 21:33:14.824: D/Class(8127): ENGLISH 10 2 OF 2
07-21 21:33:14.824: D/Teacher(8127): GURALNICK
07-21 21:33:14.824: D/Band(8127): FSS84
07-21 21:33:14.824: D/Number(8127): 8
07-21 21:33:14.824: D/Class(8127): SPANISH II 2 OF 2
07-21 21:33:14.824: D/Teacher(8127): RATCLIFFE
07-21 21:33:14.824: D/Title(8127): Tarea para 6/3
07-21 21:33:14.824: D/Date(8127): 2014-06-03
07-21 21:33:14.824: D/Type(8127): Homework
07-21 21:33:14.824: D/Description(8127): 18265
07-21 21:33:14.834: D/Description(8127):  1. The last page of your PBA review packets both readings.
07-21 21:33:14.834: D/Description(8127):  
07-21 21:33:14.834: D/Description(8127): ANSWER QUESTIONS IN COMPLETE SENTENCES.
07-21 21:33:14.844: D/Band(8127): HGS44
07-21 21:33:14.844: D/Number(8127): 10
07-21 21:33:14.844: D/Class(8127): GLOBAL STUDIES 10 2 OF 2
07-21 21:33:14.844: D/Teacher(8127): Anderson
07-21 21:33:14.844: D/Title(8127): Intro Paragraph and EDAC
07-21 21:33:14.844: D/Date(8127): 2014-06-03
07-21 21:33:14.844: D/Type(8127): Homework
07-21 21:33:14.844: D/Description(8127): 18413
07-21 21:33:14.844: D/Description(8127):  For homework tonight, please finish your introductory paragraph and any remaining EDAC sheets for your essay. Come to class tomorrow with a printed copy (in print, no phones!!!) of your intro and EDAC sheets finished for each of your body paragraphs.
07-21 21:33:14.844: D/Description(8127): Have a great night!
07-21 21:33:14.854: D/Band(8127): MRS22
07-21 21:33:14.854: D/Number(8127): 7
07-21 21:33:14.854: D/Class(8127): ALGEBRA 2 / TRIG 2 OF 2
07-21 21:33:14.854: D/Teacher(8127): KLEIN
07-21 21:33:14.854: D/Title(8127): HW #28
07-21 21:33:14.854: D/Date(8127): 2014-06-03
07-21 21:33:14.854: D/Type(8127): Homework
07-21 21:33:14.854: D/Description(8127): 18330
07-21 21:33:14.854: D/Description(8127):  A.  pg. 803 #24,25,28,30,31
07-21 21:33:14.854: D/Description(8127): B.  Check your answers here.
07-21 21:33:14.854: D/Description(8127): www.beaconschool.org/~sklein/Algebra2/a2hwtriglos.pdf
07-21 21:33:14.864: D/Description(8127): C.  Create the FIRST half of your Study Sheet for the Trig Test on Monday, 6/9/14. 
07-21 21:33:14.864: D/Description(8127):   

编辑: 所以我试图在解析器中添加一个while循环,而循环看起来收集了所有的&#34;描述&#34;并将它们全部添加到一个字符串中。我没有太多的运气,因为它似乎永远不会做我想做的事情。它会跳过文件的所有第一部分,这是应该做的,然后它会得到&#34;描述&#34;部分在一起,但之后一切都变得疯狂,它将所有这些额外的东西添加到StringBuilder。无论如何,这里是新的解析器代码及其在日志中的输出。

StringBuilder DescriptionAll = new StringBuilder();

            InputStream is = new ByteArrayInputStream(Due_Today.getBytes());


            BufferedReader reader = new BufferedReader(new InputStreamReader(is));



            try {

                while ((Due_Today = reader.readLine()) != null) {   
            String[] part = Due_Today.split("\",\"", -1);
            int noOfItems = part.length;
            int counter=0;


            DescriptionCheck = counter<noOfItems ? part[counter] : "";counter++;

            if (!Due_Today.contains("\",\"") && DescriptionCheck != null && !DescriptionCheck.isEmpty() ){

                while ((Due_Today = reader.readLine()) != null & !DescriptionCheck.contains("\",\"")) {



                    Log.d("DescriptionCheck", DescriptionCheck );

                    String[] parter = Due_Today.split(System.getProperty("line.separator"), -1);



                Description = parter[0];

                DescriptionAll.append((System.getProperty("line.separator") + Description));

                Log.d("descriptioncheck", Description );

                if (Description.contains("\",\"")) {


                    Log.d("BROKEN?", "YES" );

                    break;


                }

            }
            }

            Band = counter<noOfItems ? part[counter] : "";
            counter++;
            Number = counter<noOfItems ? part[counter] : "";
            counter++;
            Class = counter<noOfItems ? part[counter] : "";
            counter++;
            Teacher = counter<noOfItems ? part[counter] : "";
            counter++;
            Title = counter<noOfItems ? part[counter] : "";
            counter++;
            Date = counter<noOfItems ? part[counter] : "";
            counter++;
            Type = counter<noOfItems ? part[counter] : "";
            counter++;
            Data = counter<noOfItems ? part[counter] : "";
            counter++; 






            Band = Band.replaceAll("^\"|\"$", "");
            Number = Number.replaceAll("^\"|\"$", "");
            Class = Class.replaceAll("^\"|\"$", "");
            Teacher = Teacher.replaceAll("^\"|\"$", "");
            Title = Title.replaceAll("^\"|\"$", "");
            Date = Date.replaceAll("^\"|\"$", "");
            Type = Type.replaceAll("^\"|\"$", "");
            Data = Data.replaceAll("^\"|\"$", "");






        Description =   DescriptionAll.toString();

        Description = Description.replaceAll("^\"|\"$", "");

            Log.d("Data", Data);
            Log.d("Band", Band);
            Log.d("Number", Number);
            Log.d("Class", Class);
            Log.d("Teacher", Teacher);
            Log.d("Title", Title);
            Log.d("Date", Date);
            Log.d("Type", Type);
            Log.d("Description", Description);
            Log.d("DescriptionAll", Description);




            due_today_list.add(new Due_Today_List(Data, Band, Number, Class, Teacher, Title, Date, Type, Description));

here就是现在的日志,纯粹的疯狂:http://pastebin.com/M8ZT07ii

编辑:

这里有一个截图,显示除了&#34之外的所有内容完美排序时应用的样子。描述&#34; (只显示每个描述的第一行描述),这使用我尝试的第一个代码(没有新的while循环&#34;

enter image description here

在这里你可以看到它只显示&#34;描述

的第一行

enter image description here

这是我在使用带有while循环的新代码时应用程序的样子

enter image description here

这里&#34;描述&#34;输出是带有while循环的新代码,正如你可以看到while循环的结果我删除了&#34;描述&#34;的第一行,这是好的,因为它只是一个占位符< / p>

enter image description here enter image description here

enter image description here enter image description here

2 个答案:

答案 0 :(得分:1)

我认为问题来自于您使用readLine来读取文件。通过使用此功能,读取缓冲区直到换行&#34; \ n&#34;或回车&#34; \ r&#34;找到了。但是,你想要的是每次出现时分割字符串&#39;,&#39;找到了。

尝试使用read来阅读Due_Today。这只是一个伪代码:

int value   = 0;
int counter = 0;
String str  = "";
StringBuilder strb = new StringBuilder();

// Read until the end of the file
while((value = reader.read()) != -1)
{
    // Convert int to a character
    char c = (char)value;
    strb.append(c);

    // If the separator is found
    if (c == ','){

        str = strb.toString().replaceAll("^\"|\"$", "");
        switch (counter) {

            /* Do things according to the value of counter:
            Read description, data, title.. according to the data structure of what you are trying to parse 

            Example:
            case 1:  Description = str;
            break;
            case 2:  Band = str;
            break;
            */

        }

        // Prepare to read a new string and initialize the string builder
        strb.setLength(0);
        counter ++;
    }
}

我确实知道您正在解析的文件的数据结构是什么,但如果您必须在一个文件中读取多个对象,则可能会遇到一些问题。我注意到&#34;描述&#34;可以包含一些将被视为分隔符的逗号。因此,您可以只读取一个对象并说“#34;一旦我开始阅读说明,无论是否有一些逗号,都要读取文件直到结尾”#34;或者使用未在字符串内容中使用的字符更改分隔符。

希望这有帮助

修改

1)搜索你的分隔符怎么样?它可能不是一个完美的方法,但至少它很快,因为它不会在每个字符处搜索整个分隔符模式。

while((value = reader.read()) != -1)
{
     // Convert int to a character
     char c = (char)value;


      // Searching for the separator
      if (c == ','){
           if (state == 1) {
                state = 2;
           } else {
                state = 0;
                strb.append(c);
            }
       }
       else if (c == '"'){
            if (state == 2){
                 // The separator if found, read the string
                 processElement(strb, counter); 

                  // Prepare to read a new string
                  state = 0;
                  counter ++;

             } else {
                  state = 1;
                  strb.append(c);
             }
       } else {
             strb.append(c);
       }
}

// Process the last element
processElement(strb, counter);


public void processElement(StringBuilder strb, int counter){
    String str = strb.toString().replaceAll("^\"|\"$", "");
    System.out.println("String to process= " + str);

    /*
         Do something with it here..

    switch (counter) {
        case 1:  Description = str;
        break;

        case 2:  Band = str;
        break;

        ...
    }
    */

    // Initialize the string builder
    strb.setLength(0);
}

2)如果你愿意阅读文件直到结束,你可以这样做:

while((value = reader.read()) != -1)
{
     // Convert int to a character
     char c = (char)value;
     strb.append(c);
}
String str = strb.toString();

答案 1 :(得分:0)

所以我认为你正在将String解析成更小的段,这比它需要的更多。

以下是我要做的事:

一种。我会使用Files类,特别是readAllBytes方法,将整个文件的内容读入一个字节数组。

喜欢这样

import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;

Path path = Paths.get("path/to/file");
byte[] myArray = Files.readAllBytes(path);

B中。在读入文件后,我将byte []转换为String。

喜欢这样

String myString = new String(myArray);

℃。最后我会使用String函数Split。

String[] myStringArray = myString.spilt(",");

myStringArray将保留所有相关的“子串”,并随意使用。