Program wczytujący, zapisujący i kopiujący pliki

0

Komentarz wydaje mi się zbędny. W skrócie doszedłem do pewnego punktu i dalej nie idzie. Funkcje powinno dać się wywołać w postaci komend w konsoli, taki jest cel. Klasa FileManager działa poprawnie !

klasa Start

import java.io.Console;

public class Start {
	public static void main(String[] args) {
			FileManager fw = new FileManager();
			Console con = System.console();
			if (con == null) {
				System.out.println("Brak konsoli");
				System.exit(-1);
			}
			args[0] = "";
			args[0] = con.readLine("Wprowadź komendę (write, read lub copy): ");
			con.printf("Wybrany tryb: " + args[0]);
			
			if (args[0] == "read") {			
				if (args.length < 1) {
					System.out.println("Wywołanie programu: Start nazwa_pliku");
					System.exit(0);
				}
				fw.readFile(args[1]);
			}
			if (args[0] == "write") {
				if (args.length < 1) {
					System.out.println("Wywołanie programu: Start nazwa_pliku");
					System.exit(0);
				}
				fw.writeFile(args[1]);
			}
			if (args[0] == "copy") {
				if (args.length < 2) {
					System.out.println("Wywołanie programu: " + 
					"Start nazwa_pliku_zróbłowego nazwa_pliku_docelowego");
					System.exit(0);
				}
				fw.copyFile(args[1], args[2]);
			}
	}
}

klasa FileManager

public class FileManager {
		
	public void writeFile(String a) {
		String line = "";
		FileOutputStream outs = null;							
		try {
			outs = new FileOutputStream(a);
		} catch(FileNotFoundException e) {
			System.out.println("Błąd poczas otwierania pliku");
			System.exit(-1);
		}
		DataOutputStream dataout = new DataOutputStream(outs);
		BufferedReader inpstr = new BufferedReader(new InputStreamReader(System.in));
		try {
			while(true) {
				if((line = inpstr.readLine()) == null || line.equals("exit")) {
					break;
				}
			dataout.writeBytes(line + '\n'); 
			}
		} catch(IOException e) {
			System.out.println("Read/Write error.");
		}
	
	}
	public void readFile(String a) {
		String line = "";
		FileInputStream finpstr = null;
		try {
			finpstr = new FileInputStream(a);
		} catch(FileNotFoundException e) {
			System.out.println("Brak pliku");
			System.exit(-1);
		}
		DataInputStream dataout = new DataInputStream(finpstr);
		BufferedReader inpstr = new BufferedReader(new InputStreamReader(finpstr));
		try {
			while((line = inpstr.readLine()) != null) {
				System.out.println(line);	
			}
		} catch(IOException e) {
			System.out.println("Błąd wejścia - wyjścia.");
		}
	}
	public void copyFile(String a, String b) {
		FileInputStream finpstr = null;
		FileOutputStream outs = null;
		try {
			finpstr = new FileInputStream(a);
		} catch(FileNotFoundException e) {
			System.out.println("Brak pliku: " + a);
			System.exit(-1);
		}
		try {
			outs = new FileOutputStream(b);
		} catch(FileNotFoundException e) {
			System.out.println("Nie można utworzyć pliku: " + b);
			System.exit(-1);
		}
		try {
			int c;
			while((c = finpstr.read()) != -1)  {
				outs.write(c);	
			}
			System.out.println("Kopiowanie zakończone");
		} catch(IOException e) {
			System.out.println("Błąd wejścia - wyjścia.");
		}
	}
}
2

Ale co ty w ogóle wyprawiasz? Jakieś przypisywanie wartości do args[] WTF? Koszmar jakiś. A do tego w ogóle nie napisałeś CO nie działa.

dalej nie idzie

Faktycznie to wszystko wyjaśnia ;]

Komentarz wydaje mi się zbędny

Odpowiadając w duchu pytania:
To wystarczy ze poprawisz ten kod i zadziała! Komentarz wydaje mi się zbędny.

1

Pangeon troszkę namieszałeś :) Następnym razem postaraj się bardziej określić w jakim miejscu masz problem.

Komentarz wydaje mi się zbędny. W skrócie doszedłem do pewnego punktu i dalej nie idzie.

Takie sformułowanie nic nikomu nie mówi. I nikomu nie chce się szukać co miałeś na myśli.

Ale do rzeczy. Po krótkiej analizie doszedłem do mojego rozwiązania problemu:
W Twoim kodzie zrobiłes coś takiego:

public class Start {
    public static void main(String[] args) {
            FileManager fw = new FileManager();
            Console con = System.console();
            if (con == null) {
                System.out.println("Brak konsoli");
                System.exit(-1);
            }
            args[0] = "";
            args[0] = con.readLine("Wprowadź komendę (write, read lub copy): ");
            con.printf("Wybrany tryb: " + args[0]);

Tak jak Shalom wspomniał, narobiłeś niezłego bigosu.
Zmień na:

public class Start {
    public static void main(String[] args) {
        
        Console con = System.console();
        if( args.length == 0 )
        {
            con.writer().println( "Brak argumentow. Uruchom program z argumentami: write, read lub copy" );
            System.exit( -1 );
        } 
            FileManager fw = new FileManager();

            con.printf("Wybrany tryb: " + args[0]);
 

Mam nadzieję, że o to ci chodizło. Jeśli nie - wyjaśnij gdzie i jaki masz problem :)

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