Advertisement
Guest User

java_io

a guest
Sep 20th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. package java_io;
  2. //import java.util.Scanner;
  3. import java.io.*;
  4. public class input_output {
  5.  
  6. protected static void pwd(File wd, String[] filename){
  7. try {
  8. System.out.println("Path: " + wd.getCanonicalPath());
  9. } catch (IOException e) {
  10. // TODO Auto-generated catch block
  11. e.printStackTrace();
  12. }
  13. }
  14.  
  15. protected static File cd(File wd, String[] dirname){
  16. if (dirname[0] == ".."){
  17. System.out.println(wd.getParentFile());
  18. return wd.getParentFile();
  19. }
  20.  
  21. //File directory = new File("dirname").getAbsoluteFile();
  22. //boolean result;
  23.  
  24. File tmp = new File(wd, dirname[0]);
  25. if(tmp.isDirectory()) //minden ágat le kell írni
  26. return tmp;
  27. else
  28. return wd;
  29. }
  30.  
  31. private static File e(File tmp) {
  32. // TODO Auto-generated method stub
  33. return null;
  34. }
  35.  
  36. protected static void ls(File wd, String[] filename){
  37. File[] listOfFiles = wd.listFiles();
  38. for(int i = 0; i < wd.list().length; i++){
  39. System.out.println("Files: " + listOfFiles[i].getName());
  40. }
  41. }
  42.  
  43. protected static void mkdir(File wd, String[] dirname) throws IOException{
  44. /// boolean result = false;
  45. File dir = new File("dirname");//???
  46. try{
  47. if(!dir.exists()){
  48. dir.mkdir();
  49. System.out.println("Directory is created.");
  50. }
  51. else{
  52. System.out.println("Directory is not created.");
  53. }
  54. }
  55. catch(Exception e){
  56. e.printStackTrace();
  57. }
  58. }
  59.  
  60. protected static void cp(String[] filename){
  61. File source = new File(filename[0]);
  62. File dest = new File(filename[1]);
  63.  
  64. byte[] bytes = new byte[1024];
  65.  
  66. try{
  67. if(source.exists()){
  68. FileInputStream fis = new FileInputStream(source);
  69. FileOutputStream fos = new FileOutputStream(dest);
  70. while(fis.read(bytes) != -1){
  71. fos.write(bytes);
  72. fos.flush();
  73. }
  74. fis.close();
  75. fos.close();
  76. }
  77. }
  78. catch(Exception e){
  79. e.printStackTrace();
  80. }
  81. }
  82. protected static void head(int n, File wd, String[] filename){
  83. try{
  84. File t = new File (wd, filename[0]);
  85. FileReader fr = new FileReader("wd");
  86. //String s;
  87. BufferedReader br = new BufferedReader(fr);
  88. //int counter = n;
  89. while(n > 0 && br.readLine() != null){
  90. System.out.println(br);
  91. n--;
  92. }
  93. }
  94. catch(Exception e){
  95. System.out.println(e.getMessage());
  96. }
  97. }
  98.  
  99. public static void main(String[] args) throws IOException{
  100. System.out.println("Add meg a szavakat!");
  101.  
  102. // Scanner input = new Scanner(System.in);
  103. // String array;
  104. // String[] delimiter = array.split(" ");
  105.  
  106. InputStreamReader isr = new InputStreamReader(System.in);
  107. BufferedReader br = new BufferedReader(isr);
  108. String line = "";
  109. File wd = new File(System.getProperty("user.dir"));
  110.  
  111. while (line != "0"){
  112. line = br.readLine();
  113. if(line == null) break;
  114. String[] parts = line.split(" "); // parts[0] - parancs, többi arg.
  115.  
  116. if(parts[0].equals("exit")){
  117. break;
  118. }
  119. if(parts[0].equals("pwd")){
  120. pwd(wd, parts);
  121. }
  122. if(parts[0].equals("cd")){
  123. cd(wd, parts);
  124. }
  125. if(parts[0].equals("ls")){
  126. ls(wd, parts);
  127. }
  128. if(parts[0].equals("mkdir")){
  129. mkdir(wd, parts);
  130. }
  131. if(parts[0].equals("cp")){
  132. cp(parts);
  133. }
  134. if(parts[0].equals("head")){
  135. head(10, wd, parts);
  136. }
  137. System.out.println();
  138. }
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement