Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

File reader and int to char converting  RSS feed

 
Csaba Kassitzky
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone!
I would like to know how it is possible, if i'm using a FileReader in and FileWriter out with:


1. How can i convert "c" into a character(this is the important question)?
note: i need to know if c is one of the following four: < > . "linebreak"
2. How can i concat c-s into a long array, then write them in one cycle(the array is unknown length, maybe i should use a Vector)?

Thanks
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) The int returned by read will either be -1, or it will be a valid character. Therefore, you can just use a cast:



2) Although in general it's best to avoid processing data one character at a time if you can, you can just use a StringBuffer and append the characters to it one at a time:



Call toString() on the StringBuffer to get the results as a String.
 
Csaba Kassitzky
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems to me that the string i use for concatenating characters retain linebreaks as well. That is good, but if needed, how can i remove them? The best sollution would be to use another String, where i won't add linebreaks. But how can i check if a character is a linebreak or not?
[ December 10, 2005: Message edited by: Csaba Kassitzky ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some file systems put \n\r at the end of everyline, and some only put \n. To leave these out ...

Compare each character to '\n' or '\r' in the shouldAppendToBuffer method suggested above. Rather than completely leaving these characters out, you might want to append a space in the place of \n and leave out \r.
 
Csaba Kassitzky
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
\n\r, do these come together, or one of them?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Typically a line separator can be any of these:
  • A lone carriage return '\r' character
  • A lone newline '\n' character
  • A '\r' followed immediately by '\n' - these two characters together count as one line separator.

  • I'm not aware of any systems that would consider "\n\r" as a single line separator, though it might well be considered to be two line separators.

    If this seems needlessly confusing, that's because it is. Unfortunately different systems developed different ways of representing line separators, and they never got fully unified. So nowadays we tend to treat the three representations above as equivalent. More history and discussion can be found here.

    In Java, you can avoid having to think much about this by using either a BufferedReader or a Scanner to read one line at a time. These classes will locate line separators for you, and will not include the line separator as part of the line. So if you're copying the lines somewhere, you can choose whether you want to re-insert line separators in between the lines. For example:
    This will copy the contents from inFile to outFile - but it will remove all line separators in the process.

    Note that if an exact copy of a file is desired, it's better to use InputStreams and OutputStreams rather than Readers and Writers. But if you want to understand the file contents as characters and perhaps modify the file along the way, that's when Readers and Writers would be more useful.
    [ December 10, 2005: Message edited by: Jim Yingst ]
     
    Todd Patrick
    Ranch Hand
    Posts: 31
    Android Firefox Browser Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jim Yingst: My question stems from your statement "Note that if an exact copy of a file is desired, it's better to use InputStreams and OutputStreams rather than Readers and Writers. But if you want to understand the file contents as characters and perhaps modify the file along the way, that's when Readers and Writers would be more useful."

    I am working on a project that I do understand the file contents as characters and I do modify the file.

    I use a FileReader and FileWriter.

    How do I add the linebreaks back into the file?

    Thanks,
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!