Fail to split downloaded txt file

Put your problem here if it does not fit any of the other categories.

Fail to split downloaded txt file

Postby Morgrom » Thu Mar 03, 2011 2:37 pm

I have a String that I try to split. The following code works
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. lsSagor = "some text\n Some more text\n More text~Text again\n Text\n text~Some text ..."
  2.  
  3. final String[] laList = lsSagor.split("~");
  4.  
  5. String[] laSaga = laList[0].split("\n");
  6.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

Gives:
laSaga[0] => some text
laSaga[1] => some more text
laSaga[2] => More text

But if I download the textfile, it fails to split and gives:
laSaga[0] => "some text\n Some more text\n More text"
So it seems the first split works, but not the second.

There is the code I use to download the file
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. String lsSagor = getFileFromUrl(BASEURL+"/sagor.txt");
  2.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public static String getFileFromUrl(String url)
  2.     {
  3.                 InputStream content = null;
  4.                 try
  5.                 {
  6.                         HttpGet httpGet = new HttpGet(url);
  7.                         HttpClient httpclient = new DefaultHttpClient();
  8.                         // Execute HTTP Get Request
  9.                         HttpResponse response = httpclient.execute(httpGet);
  10.                         content = response.getEntity().getContent();
  11.         }
  12.                 catch (Exception e)
  13.                 {
  14.                         //handle the exception !
  15.                 }
  16.                 BufferedReader rd = new BufferedReader(new InputStreamReader(content), 4096);
  17.                 String line;
  18.                 StringBuilder sb =  new StringBuilder();
  19.                 try {
  20.                         while ((line = rd.readLine()) != null) {
  21.                                         sb.append(line);
  22.                         }
  23.                 } catch (IOException e) {
  24.                         // TODO Auto-generated catch block
  25.                         e.printStackTrace();
  26.                 }
  27.                 try {
  28.                         rd.close();
  29.                 } catch (IOException e) {
  30.                         // TODO Auto-generated catch block
  31.                         e.printStackTrace();
  32.                 }
  33.                 return sb.toString();
  34.                
  35.        
  36.     }
  37.  
  38.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
Morgrom
Freshman
Freshman
 
Posts: 2
Joined: Thu Mar 03, 2011 2:18 pm

Top

Re: Fail to split downloaded txt file

Postby jonbonazza » Wed Mar 09, 2011 10:35 pm

Make sure that the text file you are downloading uses Unix-based end of line character format.
jonbonazza
Master Developer
Master Developer
 
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

Re: Fail to split downloaded txt file

Postby Morgrom » Thu Mar 10, 2011 2:46 pm

The desktop is Linux, the server is Linux and I actually write "\n" in the textfile. It seems that \n is a bad character to use.

I changed the code to use # instead of \n and than replace # with \n for output:

So the textfile looks like this "some text# more text# and more text ~some text ..."

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. String lsSagor = getFileFromUrl(BASEURL+"/sagor.txt");
  2.                         String lsSagor = getFileFromUrl(BASEURL+"/sagor.txt");
  3.                
  4.                 final String[] laList = lsSagor.split("~");
  5.        
  6.                 giAntalSagor = laList.length;
  7.                
  8.                 //String saga = laList[0].replaceAll("#", "\n");
  9.                 String[] laSaga = laList[0].split("#");
  10.                
  11.                 final String[] guiLaList = new String[giAntalSagor];
  12.                 for (int i = 0; i < giAntalSagor; i++)
  13.                 {
  14.                         guiLaList[i] = laList[i].replaceAll("#", "\n");
  15.                 }
  16.                
  17.                 changeSpelandesSaga(laSaga[0]);
  18.  
  19.  
  20.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
Morgrom
Freshman
Freshman
 
Posts: 2
Joined: Thu Mar 03, 2011 2:18 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 17 guests