i= Integer.parseInt(dis.readLine()) [Deprecated].. How to resolve it? [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have encountered the following error while compiling my Demo code on DataInputStreamDemo:
error:
i= Integer.parseInt(dis.readLine()) [Deprecated]
//where dis = reference DataInputStream obj

Reason for the method being deprecated :
This method does not properly convert bytes to characters.
Solution
Existing code : DataInputStream d = new DataInputStream(in);
Modified code : BufferedReader d
= new BufferedReader(new InputStreamReader(in));
refrences

The javadoc for this method makes it reasonably clear why it's deprecated, and has been for a long time, and suggests a better alternative.
Quoting from the Javadoc:
This method does not properly convert bytes to characters. As of JDK 1.1, the preferred way to read lines of text is via the BufferedReader.readLine() method. Programs that use the DataInputStream class to read lines can be converted to use the BufferedReader class by replacing code of the form:
DataInputStream d = new DataInputStream(in);
with:
BufferedReader d
= new BufferedReader(new InputStreamReader(in));

Related

Call a C program in Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I know there is already some posts about this but none of them answered my question.
Here's the situation, I have program written in C but I developped my GUI in java. So what I need to do, is calling my C program when I click on a button or a combobox.
In order to do that, is it possible to just compile the C program and then call it in my java interface ? (and if it is possible, how do I do that ?).
Or do I have to use JNI ? (I've read some posts about it but it seemed quite complicated to learn it for just one project).
To have maximum interoperability you should use JNI, but as a simple approach just compile C code and call C executable from JAVA with Runtime.getRuntime().exec().
if you are under UNIX to read response you could do something like this:
String line;
Process p = Runtime.getRuntime().exec( "/path/to/C/executable" );
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();

Is there a way on getting Scanner input without having to create an instance of the class? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Let's say I have this code:
Scanner scan=new Scanner(System.in);
char x = 'h', aux;
System.out.println("Insert a single letter");
aux=scan.nextLine().charAt(0);
if (x==aux)System.out.println("match");
Is there a way to avoid
Scanner scannerobject = new Scanner();
and get the work done with a function tool like C's scanf() or fget()?
I was just confused about this!
You don't have to use a Scanner to get input in Java. Scanner is just a high level class for parsing input while reading it. You can read from the static InputSteam System.in. Your probably want to wrap it in to with a BufferedReader though since you likely want to deal with Strings more than byte arrays.
e.g.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
You can use a Scanner object to get input from console or from files to my knowledge but maybe more sources. If your method / class was doing both one after another repeatedly it would be useful to have separate objects to do so. Otherwise all of the configuration would have to be done each time you wanted to get input;

Java: looking for chars in a binary file [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I need to look into a binary file.
I need to parse it in order to read some chars that the file contains.
Please any hint of how could I do it?
Thanks!
To be more specific I am looking for a particular sequence of chars in the file.
I suggest using a BufferedReader:
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
int index;
while(line = br.readLine() != null) {
index = line.indexOf(string);
if (index != -1) break;
//Assuming your file has some sort of key/value data
}

New line in different languages [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I've encountered a problem while working on my testing system. I'm creating a file in Java, writing to it in Java, but reading from it in Pascal compilators.
So, this may be not unclear, but when I do something like this in Java (Eclipse)
File file = new File("D:/i.txt");
BufferedReader bf = new BufferedReader(new FileReader(file));
PrintWriter pw = new PrintWriter(new FileWriter(file));
pw.print("hey\n");
pw.print("you");
bf.close();
pw.close();
It gives me a file that looks like
hey
you
And when I run this code on Pascal language
begin
assign(input,'D:/i.txt'); reset(input);
while not eoln(input) do write(1);
end.
Which means: Write "1" until you find a new line separator.
It won't stop to write ones.
But this is okay. Here is another strange thing: Pascal must have a line break or a line separator, or new line indicator and I found this to be Char number 10 on ASCII table (LF, new line).
So, I decided to do other way.
File file = new File("D:/i.txt");
BufferedReader bf = new BufferedReader(new FileReader(file));
PrintWriter pw = new PrintWriter(new FileWriter(file));
pw.print("hey"+(char)10);
pw.print("you");
bf.close();
pw.close();
This one would give me the same output file as the first bit of code ( at last apparently ).
But all my Pascal compilators still keep complaining and writing hundreds of ones.
How can I solve the problem with new lines?
Thank you.
I think the infinite loop (writing hundreds of ones) is because you never read anything from the input so it is never at the end-of-line. Try putting a read(input,ch); in the loop.

Efficient Way to Read a File Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
What is the most efficient way to read input from a file ?
I have a very very large file which contains a list of words separated by a newline
e.g
computer
science
is
fun
really
I was thinking about using a BufferedReader object however I was confused by this line in the documentation.
"In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders. For example,
BufferedReader in = new BufferedReader(new FileReader("foo.in"));
will buffer the input from the specified file. Without buffering, each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient. " <
Can some please explain this to me?
On second read I starting to believe the BufferedReader is my best bet. Is there a better way?
This post may help.
BufferedReader is a good choice, letting you turn a BufferedReader into a java.util.Stream in Java 8.
Parsing a large CSV file for instance with java.util.stream package:
InputStream is = new FileInputStream(new File("persons.csv"));
BufferedReader br = new BufferedReader(new InputStreamReader(is));
List<Person> persons = br.lines()
.substream(1)
.map(mapToPerson)
.filter(person -> person.getAge() > 17)
.limit(50)
.collect(toList());
Unlike Collections which are in-memory data structures which hold elements within it , Streams allow parallel processing and behave like fixed data structures which computes the elements on-demand basis.
Moreover, Streams also support Pipelining and Internal Iterations.

Categories

Resources