Solved

reading text file problem

Posted on 2006-07-06
13
242 Views
Last Modified: 2010-03-31
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
Comment
Question by:komputer
  • 5
  • 5
  • 3
13 Comments
 
LVL 92

Expert Comment

by:objects
ID: 17049534
use utf8 encoding to store it
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17049535
You need to use UTF8 in the db
0
 
LVL 92

Expert Comment

by:objects
ID: 17049537
generally you can specify the encoding oin your connection string
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:komputer
ID: 17049560
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
 
LVL 92

Accepted Solution

by:
objects earned 75 total points
ID: 17049575
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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 50 total points
ID: 17049633
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
 

Author Comment

by:komputer
ID: 17049681
  >>you'll also need to specify ewncoding when saving to database

could you send me the needed connection string please?

0
 
LVL 92

Expert Comment

by:objects
ID: 17049690
depends on your database
0
 

Author Comment

by:komputer
ID: 17049788
for oracle?
0
 
LVL 92

Expert Comment

by:objects
ID: 17049819
don't think you need anything special for oracle :)
0
 

Author Comment

by:komputer
ID: 17049910
thanks guys...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17049978
:-)
0
 

Author Comment

by:komputer
ID: 17050021
justice for all ;)
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question