2013年9月15日星期日

Seek expert coaching a Java code to run the results are not the reason ..

Class "ArrayList entity : logData objects being stored after " function ....
source code is as follows :

import java.io.BufferedReader;   
import java.io.FileReader;   
import java.io.IOException;   
import java.util.ArrayList;
   
public class ReadTxt {   
  static ArrayList<String> logData = new ArrayList<String>();
static String text = null;
    public static void read(String filePath) {   
         try {   
              FileReader input = new FileReader(filePath);   
              BufferedReader br = new BufferedReader(input);   
              while (( text = br.readLine()) != null) {   
               logData.add(text);
              }   
              br.close();   
              input.close();   
        } catch (IOException e) {   
              e.printStackTrace();   
        }   
     }   
}  

after the following code is my brother conducted a test class :
brother I had wanted to be a hard disk data in the file is read into memory , and then output to a file ...
test class source code is as follows :

public class Test32 {
static String[] str1 = null;
static String[] str8 = null;
static String str10 = null;
static String Path = "D:\\MyDoc\\Dport\\MakeaTry.txt";
static int len = -1;
public Test32(){
String str = "E:\\8.txt";
ReadTxt.read(str);
len = ReadTxt.logData.size();
str8 = new String[len];
for(int i=0;i<len;i++){
str8[i]=ReadTxt.logData.get(i);
}
}
public static void main(String[] args){
new Test32();
for(int i=0;i<len;i++){
str10 =str10+str8[i];
}
str1 = str10.split(".");
int len1 = str1.length;
System.out.println(len1);
for(int i = 0;i<len1;i++){
    WriteTxt.list.add(str1[i]);
}
}
}

However , I get the final output is:

master, I was testing on line 18 class constructor call me , and, in the first 23 rows of the output .
my data in the file is the " one string " " Total 200 string data " "Every one string using a". " separated " I wrote the code by a brother , you should be able to get:
output data should be:
200 , right ?
Why is my code here, brother , and brother I expected , resulting in inconsistent ?
hope to get a master 's coaching:
Here " expected output effect is inconsistent with the " What is the reason ... ?
Thank you, master ! !
hundred points offer ! !
a perfect running day and night to build toward the dreamer
September 15, 2013 Sunday 6:39
--- --- Solution --------------------------------------------
str10 = str10 + str8 [i]; this place to add a separator to str10 = str10 + "." + str8 [i]; behind str1 = str10.split (" . " ) ; instead str1 = str10.split (" \ \. "); escape , " . " is a regular expression .
----- - Solution --------------------------------------------
see it split the API, public String [] split (String regex)
him it is received by a regular expression , and the. is a regular expression metacharacters , if you use it for segmentation,
must be escaped \ \ .

------ For reference only ---------------------------------- -----
top one first . . . . . . . . . Are watching .
------ For reference only -------------------------------------- -
landlord preceding code are nothing big issue should be str1 = str10.split ("."); out of the question
try with spaces str1 = str10.split ("");
The reason output 0 , it should be no documentation " ." character , so the output is 0

------ For reference only ----- ----------------------------------
OKay! !
Thank you for last night, is your brother ! !
Thank wind_elf brother on the head ! !
wind_elf brother ..
As the " last night that you" brother, is already " the core layer is the brother of my questions allies" , and the , and his reply is indeed a brother I get most knowledge ..
brother I ..
will score the most , 80 minutes, to "Last night is your " brother of ...
brother I'll give you 20 points , hope you do too few ..
Japan to have a chance , my brother , must make up the score ! !
caofeng891102 brother ..
Due to time constraints, brother me, click on the "knot affixed to the sub ", the only thought : your "top up " for the help of love ..
okay, Japan, longer , your name , brother I have in mind , I will forward your favor , in the future, look for opportunities to make up ! !
Thank you brother for the help ! !

------ For reference only ---------------------------------------
sorry ...
constantly swimming fish brother , brother I ...
of time in replies ..
get your help solve the problem ..
your nickname , brother I've also written down ...
Japan, longer , some of you will opt to her brother's friendship, filled ! !
------ For reference only -------------------------------------- -
brother my WriteTxt class code is as follows:


import java.io.BufferedWriter;   
import java.io.FileNotFoundException;   
import java.io.FileWriter;   
import java.io.IOException;   
import java.util.ArrayList;   
import java.util.List;   
   
public  class WriteTxt{   
    static ArrayList<String> list=new ArrayList<String>();
    
       public static void write(String filePath){   
                
             try {   
                  FileWriter output = new FileWriter(filePath);   
                  BufferedWriter  bf= new BufferedWriter(output);   
                   for(String l:list ){   
                        bf.write(l+ "\r\n");   
                  }   
            bf.flush();//此处很关键,如果不写该语句,是不能从缓冲区写到文件里的   
            } catch (FileNotFoundException e) {   
                  e.printStackTrace();   
            } catch (IOException e) {   
                   // TODO Auto-generated catch block   
                  e.printStackTrace();   
            }   
               
      }
       
       public static void writeDatatoFiletail(ArrayList<String> Data,String filePath){
        FileWriter output = null;
try {
output = new FileWriter(filePath,true);  
int len = Data.size();
for(int i = 0;i<len;i++){
BufferedWriter  bf= new BufferedWriter(output);   
bf.write(Data.get(i));     
bf.flush(); 
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();

       }
       
}

没有评论:

发表评论