Results 1 to 3 of 3
Thread: Unicode string serach problem
- 07-01-2009, 07:53 AM #1
Member
- Join Date
- Jul 2009
- Posts
- 1
- Rep Power
- 0
Unicode string serach problem
Hi,
I am new in Java. Problem is the Unicode string. I have a Unicode file where I am searching a few words/sentence. The search string is defined as
String StringToBeSearch = "Because the Agent software is already";
When I am opening the file and try to compare it with the file content, it don’t succeeds. If I am opening that Unicode text file the contents are there.
What print the every line of the txt file and observed that the every latter have one more space like word “Because” is printed as “B e c a u s e”. Hence the search has failed.
public static boolean ReadFileAndSearchString()
{
String FileName = "Install.txt";
String StringToBeSearch = "Because the Agent software is already";
boolean found = false;
File file = new File(FileName);
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
try {
fis = new FileInputStream(file);
// Here BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
// dis.available() returns 0 if the file does not have more lines.
while (dis.available() != 0) {
// this statement reads the line from the file and print it to
// the console.
String line = dis.readLine();
System.out.println(line);
int ret = line.indexOf(StringToBeSearch);
if ( ret >= 0 )
{
System.out.println("Got The text");
found = true;
break;
}
}
// dispose all the resources after using them.
fis.close();
bis.close();
dis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return found;
}
also tried with the following code
FileInputStream fis = new FileInputStream(FileName);
InputStreamReader isr = new InputStreamReader(fis, "UTF8");
but did not worked.
Providing my txt file as attachment.
- 07-01-2009, 06:32 PM #2
Probably because you're using a DataInputStream. Just stick with the BufferedInputStream.
Also, you only need to close the outer-wrapping stream.Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 07-02-2009, 10:22 AM #3
Hi,
Use UTF16 instead of UTF8 and try once using InputStreamReader.
FileInputStream fis = new FileInputStream(FileName);
InputStreamReader isr = new InputStreamReader(fis, "UTF16");
-Regards
Ramya
Gothru this sample code
Java Code:import java.io.*; class Test { public static void main(String[] args) throws Exception { String FileName = "trial.txt"; String StringToBeSearch = "Because the Agent software is already"; boolean found = false; File file = new File(FileName); FileInputStream fis = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fis, "UTF16"); StringBuffer buffer = new StringBuffer(); Reader in = new BufferedReader(isr); int ch; while ((ch = in.read()) > -1) { buffer.append((char)ch); }//while in.close(); int ret = buffer.toString().indexOf(StringToBeSearch); if ( ret >= 0 ) { System.out.println("Got The text"); found = true; } // dispose all the resources after using them. in.close(); fis.close(); }//main }//classRamya:cool:
Similar Threads
-
Fedora Itext Unicode Problem
By gautamn in forum Java 2DReplies: 0Last Post: 04-13-2009, 08:12 AM -
String/sentence to unicode convertion
By sandeepvreddy in forum New To JavaReplies: 5Last Post: 11-20-2008, 03:33 PM -
SWT 2D Unicode Example
By Java Tip in forum SWTReplies: 0Last Post: 06-28-2008, 09:21 PM -
How to Draw Unicode String in Java
By Java Tip in forum java.awtReplies: 0Last Post: 06-23-2008, 11:15 PM -
Unicode problem
By rovshanb in forum JDBCReplies: 0Last Post: 02-14-2008, 06:41 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks