Results 1 to 3 of 3
  1. #1
    saurabh01 is offline Member
    Join Date
    Jul 2009
    Posts
    1
    Rep Power
    0

    Default 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.
    Attached Files Attached Files

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    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

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    828
    Rep Power
    6

    Default

    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
    
    
    }//class
    Ramya:cool:

Similar Threads

  1. Fedora Itext Unicode Problem
    By gautamn in forum Java 2D
    Replies: 0
    Last Post: 04-13-2009, 08:12 AM
  2. String/sentence to unicode convertion
    By sandeepvreddy in forum New To Java
    Replies: 5
    Last Post: 11-20-2008, 03:33 PM
  3. SWT 2D Unicode Example
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 06-28-2008, 09:21 PM
  4. How to Draw Unicode String in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-23-2008, 11:15 PM
  5. Unicode problem
    By rovshanb in forum JDBC
    Replies: 0
    Last Post: 02-14-2008, 06:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •