This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of TensorFlow 2.0 in Action and have Thushan Ganegedara on-line!
See this thread for details.
Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

in.readline deprecated

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a code that I'm working on for the client side of server using ftp. I know that in.readLine() is deprecated, but I'm not sure what to use in place of it. I tried BufferredReader.readLine(), but it threw an error.
I have three instances of in.ReadLine in my code



My code is:
 
Master Rancher
Posts: 3701
44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Taylor Manley wrote:I have a code that I'm working on for the client side of server using ftp. I know that in.readLine() is deprecated, but I'm not sure what to use in place of it. I tried BufferredReader.readLine(), but it threw an error.



Well, the DataInputStream.readLine() javadoc is pretty clear, you should use a BufferedReader instead.  If you're getting an error with that, it would help to say what that error is, so we can work on it.  

In this case though, I'm pretty sure that the next problem you're seeing is that the second time you open a BufferedReader (or DataInputStream) using System.in, it won't be able to read anything from that.  You can only open one stream on System.in, and that's the stream that will be used; streams don't know how to share.  So if your main() method needs to read from System.in, and also other methods need to read from System.in, you need to create exactly one BufferedReader for System.in, and pass that reader to the other methods that would use it.  
 
Marshal
Posts: 70690
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use data input streams for reading text. It might appear to work as long as all your characters are ≤ 0x7f (=ASCII), but as soon as you try accented letters or anything different, you won't read them correctly.
Use a Scanner to read from System.in, preferably wrapped in a utility class to deal with the poorly‑documented pitfalls around Scanner. As you will see from the method you quoted, you can use classes with names ending xxxReader for reading text. There is much more information in the Java™ Tutorials.

Please supply more details about the exception (I presume that is what is happening). Please declare the specific type of exception in line 8, not simply Exception.
 
Saloon Keeper
Posts: 22668
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Don't use data input streams for reading text. It might appear to work as long as all your characters are ≤ 0x7f (=ASCII), but as soon as you try accented letters or anything different, you won't read them correctly.
Use a Scanner to read from System.in, preferably wrapped in a utility class to deal with the poorly‑documented pitfalls around Scanner. As you will see from the method you quoted, you can use classes with names ending xxxReader for reading text. There is much more information in the Java™ Tutorials.

Please supply more details about the exception (I presume that is what is happening). Please declare the specific type of exception in line 8, not simply Exception.



Scanner (that nasty little   ) is intended for basic reading and parsing of input into tokens. And actually, it, too would have issues with input that exceeds the basic 7-bit ASCII character set unless you construct it with an appropriate charset indication.

For proper I18N processing of general text from a file, I'd recommend that you open a FileInputStream, then construct an InputStreamReader on that. Or on System.in, if using stdin. The InputStreamReader allows you to set the proper charset for your input so that stuff like accented letters get handled properly and cleanly. Note that FileReader doesn't appear to have any characterset constructors itself.

Once you have a properly-configured Reader, I recommend using it to construct an LineNumberInputReader. This gives you (non-deprecated) readLine() methods plus if you need to complain about input, you'll have the offending line number already available without having to implement line-counting logic of your own.

That can be a more complex process than simply whipping out a Scanner, but if you need to read-and-store text, process XML, or do other things that Scanner isn't good at, it's a proven process.
 
Sheriff
Posts: 22022
108
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:For proper I18N processing of general text from a file, I'd recommend that you open a FileInputStream, then construct an InputStreamReader on that. Or on System.in, if using stdin. The InputStreamReader allows you to set the proper charset for your input so that stuff like accented letters get handled properly and cleanly. Note that FileReader doesn't appear to have any characterset constructors itself.


FileReader got two constructors that take a Charset in Java 11. I've been using the FileInputStream-in-InputStreamReader workaround for ages, so I was quite glad to see these constructors finally being there.

Once you have a properly-configured Reader, I recommend using it to construct an LineNumberInputReader. This gives you (non-deprecated) readLine() methods plus if you need to complain about input, you'll have the offending line number already available without having to implement line-counting logic of your own.


I think you mean LineNumberReader. I can't remember ever using that though, BufferedReader usually works good enough for me.
 
Tim Holloway
Saloon Keeper
Posts: 22668
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the update! I didn't pay attention to which version of Java I got my JavaDoc from, but the lack of support in earlier versions was a curious omission.

And yes, I misquoted the LineNumberReader classname. I'm at the point where I can't even remember what I read on the previous browser tab. yeeks.

I have used LineNumberReader quite a bit, since I generally do want to be able to output a message when the reading process encounters bad text and knowing the line number is generally very helpful.
 
Saloon Keeper
Posts: 12431
269
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:FileReader got two constructors that take a Charset in Java 11. I've been using the FileInputStream-in-InputStreamReader workaround for ages, so I was quite glad to see these constructors finally being there.




I haven't used FileInputStream/InputStreamReader in ages. This is what I normally do:

It uses UTF-8 by default. If you don't want that, you can specify a charset explicitly.
 
Marshal
Posts: 25963
70
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:It uses UTF-8 by default. If you don't want that, you can specify a charset explicitly.



And if it happens that you want ISO-8859-1 as your charset, which is still a possibility for many people, you can specify that like this: StandardCharsets.ISO_8859_1. Doing that makes it unnecessary to catch the Yoda exception which is thrown by Charset.forName("ISO-8859-1").
 
The glass is neither half full or half empty. It is too big. But this tiny ad is just right:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic