0

I have a program that reads in a file. In this file there are some crazy chars that I have never seen before. The purpose of this file is to parse certain information into SQL statements.

When I get to this line in the file "read “Details for …(the name of the title”" (notice the horizontal ellipses and the right/left quotes), it outputs into this:

�Details for �(the name of the title�

I just want to replace the chars that are right with chars defined by me. I have tried:

st = st.replaceAll("…","...");
st = st.replaceAll("\u2026","...");

This is how i read the file:

 FileInputStream file = new FileInputStream(filePath);
 DataInputStream in = new DataInputStream(file); 
 BufferedReader br = new BufferedReader(new InputStreamReader(in));

And other things that I cant even remember. How can I do this seemingly simple task?

3
  • Can you be more specific, which file you are trying to read? (txt, xls, html etc) Commented Apr 26, 2012 at 19:34
  • 2
    So you're reading the file in the wrong encoding and want to replace arbitrary chars? Well I can't see what could go wrong with that ;) Commented Apr 26, 2012 at 19:34
  • Original file was in rft, it was then saved to txt using Word. I am trying to parse the txt file. Commented Apr 26, 2012 at 19:40

2 Answers 2

1

You need specify the encoding on read the file before replaces specials chars...

FileInputStream inputStream = new FileInputStream("input.txt");
// Specify the enconding
InputStreamReader streamReader = new InputStreamReader(inputStream, "UTF-8");
BufferedReader in = new BufferedReader(streamReader);
Sign up to request clarification or add additional context in comments.

Comments

0

Unless it's absolutely necessary you don't really have to drop those weird (yet still meaningful) characters...

Have a look at the documentation for InputStreamReader and specify the right encoding when reading your file.

2 Comments

Can you give me an example of how to call that inputStream? currently I have: FileInputStream file = new FileInputStream(filePath); DataInputStream in = new DataInputStream(file); BufferedReader br = new BufferedReader(new InputStreamReader(in));
You'll find a clear illustrated example here: docs.oracle.com/javase/tutorial/i18n/text/stream.html You will need to find out what specific encoding your .txt file is in (and pass that to the constructor).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.