• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 265
  • Last Modified:

reading text file problem

hi,

i need to read text files and store into database. i can read but i have problem with this turkish characters; ŞşİıĞğ. So i cant store properly. what should i do?

thanks...
0
komputer
Asked:
komputer
  • 5
  • 5
  • 3
2 Solutions
 
objectsCommented:
use utf8 encoding to store it
0
 
CEHJCommented:
You need to use UTF8 in the db
0
 
objectsCommented:
generally you can specify the encoding oin your connection string
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
komputerAuthor Commented:
my code to read txt file below;

          try {
            BufferedReader in = new BufferedReader(new FileReader(fileName));
            String data = "";
            while ((str =in.readLine()) != null) {
               
                    data += str;
                  }
            }
            in.close();
        } catch (IOException e) {
              
        }

where should i change?
sorry i am new at java.



0
 
objectsCommented:
change it to:

            BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), charset));

where charset is the encoding ujsed in the text file

you'll also need to specify ewncoding when saving to database
0
 
CEHJCommented:
If you want to save a file as a string it would be more efficient to do it more like below.


      public static String fileToString(String fileName) {
            Reader in = null;
            try {
                  File f = new File(fileName);
                  StringBuffer sb = new StringBuffer((int)f.length());
                  char[] charBuf = new char[BUF_SIZE];
                  in = new InputStreamReader(new FileInputStream(f), "UTF8");
                  int charsRead = -1;
                  while ((charsRead = in.read(charBuf, 0, BUF_SIZE)) > -1) {
                        sb.append(charBuf, 0, charsRead);
                  }
                  return sb.toString();
            }
            catch(IOException e){
                  e.printStackTrace();
                  return null;
            }
            finally {
                  try { in.close(); } catch(IOException e) { /* ignore */ }
            }
      }
0
 
komputerAuthor Commented:
  >>you'll also need to specify ewncoding when saving to database

could you send me the needed connection string please?

0
 
objectsCommented:
depends on your database
0
 
komputerAuthor Commented:
for oracle?
0
 
objectsCommented:
don't think you need anything special for oracle :)
0
 
komputerAuthor Commented:
thanks guys...
0
 
CEHJCommented:
:-)
0
 
komputerAuthor Commented:
justice for all ;)
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 5
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now