Odczyt i zapis do pliku

0

Siema, spotkałem się z ciekawym zapisem, a mianowicie:
java com.my.test.myprog < wej.txt > wyj.txt domyślam się, że program pobiera dane z pliku wej.txt i zapisuje je do pliku wyj.txt. Znalazłem nawet kod, który powinien wykonywać tę operację:

package my.com.test;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;

public class myprog {
	
public static void main(String args[]) {

    try {
       
       String inputFile = "wej.txt";
       String outputFile = "wyj.txt";

       // my ProcessBuilder Strings will be different from yours
       ProcessBuilder pb = new ProcessBuilder("java", "-cp", ".;bin;",
             "my.com.test.myprog");
       pb.redirectErrorStream(true); 
       Process p = pb.start();

       final OutputStream pos = p.getOutputStream();
       final PrintWriter pw = new PrintWriter(pos);
       final InputStream fis = new FileInputStream(inputFile);
       final BufferedReader fileBr = new BufferedReader(new InputStreamReader(fis));

       InputStreamReader isr = new InputStreamReader(p.getInputStream());
       final BufferedReader br = new BufferedReader(isr);

       new Thread(new Runnable() {
          public void run() {
             String lineRead;
             try {
                while ((lineRead = br.readLine()) != null) {
                   System.out.println(lineRead);
                }
             } catch (IOException e) {
                e.printStackTrace();
             } finally {
                if (br != null) {
                   try {
                      br.close();
                   } catch (IOException e) {
                      e.printStackTrace();
                   }
                }
             }
          }
       }).start();

       new Thread(new Runnable() {
          public void run() {
             try {
                String lineRead;
                while ((lineRead = fileBr.readLine()) != null) {
                   pw.println(lineRead);
                }
             } catch (IOException e) {
                e.printStackTrace();
             } finally {
                if (pw != null) {
                   pw.close();
                }
                if (fileBr != null) {
                   try {
                      fileBr.close();
                   } catch (IOException e) {
                      e.printStackTrace();
                   }
                }
             }
          }
       }).start();

    } catch (IOException e) {
       e.printStackTrace();
    }
 } // end main
}

Po kompilacji i uruchomieniu w ten sposób:

java my.com.test.myprog < wej.txt > wyj.txt

W pliku wyj.txt wyrzuca wyjątki:

Exception in thread "main" java.lang.NoClassDefFoundError: my/com/test/myprog
Caused by: java.lang.ClassNotFoundException: my.com.test.myprog
	at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: my.com.test.myprog. Program will exit.

Jakiś podpowiedzi ? Myślę, że coś jest może nie tak ze ścieżką, że nie znajduje klasy myprog.

0

Jeśli wpisujesz takie polecenie: java my.com.test.myprog, to musi być taka struktura katalogów (w stosunku do bieżącego):
podkatalog my,
w nim podkatalog com,
w nim podkatalog test,
w nim klasa myprog.
Imo, w zapisie java com.my.test.myprog < wej.txt > wyj.txt chodzi raczej o to, że polecenia czytające z System.in bedą czytały z pliku wej.txt, a polecenia wypisania na ekran System.out.println() będą pisały do pliku wyj.txt.

0

Posiadam właśnie taką strukturę katalogów, może jakieś inne sugestie ?

Co prawda pierwsza część wypowiedzi nie pomogła zbytnio, druga bardzo rozjaśniła mi działanie tego przez co problem rozwiązany. Dzięki wielkie ;)

0

Podane rozwiązanie jest poprawne. Albo masz inna strukturę katalogów niż Ci się wydaje, albo wpisując polecenie java my.com.test.myprog nie jesteś w katalogu zawierającym podkatalog my.

1 użytkowników online, w tym zalogowanych: 0, gości: 1