Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package java_io;
- //import java.util.Scanner;
- import java.io.*;
- public class input_output {
- protected static void pwd(File wd, String[] filename){
- try {
- System.out.println("Path: " + wd.getCanonicalPath());
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- protected static File cd(File wd, String[] dirname){
- if (dirname[0] == ".."){
- System.out.println(wd.getParentFile());
- return wd.getParentFile();
- }
- //File directory = new File("dirname").getAbsoluteFile();
- //boolean result;
- File tmp = new File(wd, dirname[0]);
- if(tmp.isDirectory()) //minden ágat le kell írni
- return tmp;
- else
- return wd;
- }
- private static File e(File tmp) {
- // TODO Auto-generated method stub
- return null;
- }
- protected static void ls(File wd, String[] filename){
- File[] listOfFiles = wd.listFiles();
- for(int i = 0; i < wd.list().length; i++){
- System.out.println("Files: " + listOfFiles[i].getName());
- }
- }
- protected static void mkdir(File wd, String[] dirname) throws IOException{
- /// boolean result = false;
- File dir = new File("dirname");//???
- try{
- if(!dir.exists()){
- dir.mkdir();
- System.out.println("Directory is created.");
- }
- else{
- System.out.println("Directory is not created.");
- }
- }
- catch(Exception e){
- e.printStackTrace();
- }
- }
- protected static void cp(String[] filename){
- File source = new File(filename[0]);
- File dest = new File(filename[1]);
- byte[] bytes = new byte[1024];
- try{
- if(source.exists()){
- FileInputStream fis = new FileInputStream(source);
- FileOutputStream fos = new FileOutputStream(dest);
- while(fis.read(bytes) != -1){
- fos.write(bytes);
- fos.flush();
- }
- fis.close();
- fos.close();
- }
- }
- catch(Exception e){
- e.printStackTrace();
- }
- }
- protected static void head(int n, File wd, String[] filename){
- try{
- File t = new File (wd, filename[0]);
- FileReader fr = new FileReader("wd");
- //String s;
- BufferedReader br = new BufferedReader(fr);
- //int counter = n;
- while(n > 0 && br.readLine() != null){
- System.out.println(br);
- n--;
- }
- }
- catch(Exception e){
- System.out.println(e.getMessage());
- }
- }
- public static void main(String[] args) throws IOException{
- System.out.println("Add meg a szavakat!");
- // Scanner input = new Scanner(System.in);
- // String array;
- // String[] delimiter = array.split(" ");
- InputStreamReader isr = new InputStreamReader(System.in);
- BufferedReader br = new BufferedReader(isr);
- String line = "";
- File wd = new File(System.getProperty("user.dir"));
- while (line != "0"){
- line = br.readLine();
- if(line == null) break;
- String[] parts = line.split(" "); // parts[0] - parancs, többi arg.
- if(parts[0].equals("exit")){
- break;
- }
- if(parts[0].equals("pwd")){
- pwd(wd, parts);
- }
- if(parts[0].equals("cd")){
- cd(wd, parts);
- }
- if(parts[0].equals("ls")){
- ls(wd, parts);
- }
- if(parts[0].equals("mkdir")){
- mkdir(wd, parts);
- }
- if(parts[0].equals("cp")){
- cp(parts);
- }
- if(parts[0].equals("head")){
- head(10, wd, parts);
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement