I am trying to figure out how to check for carriage returns in a string. I am reading file contents into a string variable, and need to check if x0D is present (carriage return). I have tried indexOf and matcher so far with no results.

Any input would be helpful.

Thanks

Recommended Answers

All 3 Replies

Try using '\r' with indexOf. The other thing that would help us is knowing how you're reading the file. If you are using "readLine" then \r \n \r\n are taken as line endings and stripped from the returned String.

Try using '\r' with indexOf. The other thing that would help use is knowing how you're reading the file. If you are using "readLine" then \r \n \r\n are taken as line endings and stripped from the returned String.

That is precisely my problem then. I am using readline to read the file. Forgive me, I am a .NET transplant and am new to Java. What way would I read the file so as to retain all the non-printable characters?

Thanks

One of the simple read methods.

FileInputStream fis = new FileInputStream("file");
String text = "";
byte[] buf = new byte[8192];
int read = 0;
while ((read = fis.read(buf)) >= 0) {
  text += new String(buf, 0, read);
}
fis.close();

As a simple example.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.