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
}
Related
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 5 years ago.
Improve this question
I have to read a file if some pattern occurs that file i have to read next lines of the text file in java
while (sc1.hasNextLine()){
String a = sc1.nextLine();
String b = sc1.nextLine();
if(a.contains("pattern")){
//read next lines
}
}
To simply read the file to find the line you want, just use a loop until you reach the end or the pattern your want like this :
while(sc1.hastNextLine() && !sc.nextLine().contains("pattern")){
//skip
}
then you just have to check if there is still a line to read
if (sc1.hastNextLine()){
//read the line
}
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();
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 7 years ago.
Improve this question
What should I change in my code to write my array into text file in a single line instead of a different line per vale of the array?
PrintWriter zapis = new PrintWriter("wyniki.txt");
for (int g=1; g<801; g++)
{
zapis.println(g+":"+wynik[g]+" ");
}
zapis.close();
As the documentation states, println "Prints a String and then terminates the line."
If you don't want to terminate the line, use print instead.
Change the line inside for loop like this :
zapis.print(g+":"+wynik[g]+" ");
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 8 years ago.
Improve this question
I want to build a program, which can log the programs or threads running in windows. I would like to write the program in java.
Does anyone know such mehtod?
Try like this:
Process p = Runtime.getRuntime().exec
(System.getenv("windir") +"\\system32\\"+"tasklist.exe");
BufferedReader input =
new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null)
{
System.out.println(line);
}
run "tasklist" cmd using runtime instance.
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));