How can I get the current running programs in java? [closed] - java

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.

Related

Assigning a file name to a variable 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 5 years ago.
Improve this question
I want to simply assign the file name "database.txt" to a string type variable say "fname".
What is the code for it for java.
I want to use relative file destinations.
try something like this :
File file = new File("your/path");
String str = file.getName();

reading next lines from a file after particular pattern [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 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
}

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();

Write java array to file.txt in one line [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 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]+" ");

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
}

Categories

Resources