-1

I have this methods

    OutputStream os = new FileOutputStream("out.txt");
    InputStream is = new FileInputStream("out.txt");
    Transit.outputTransport(Parking, os);
    System.out.println(Parking.toString());
    Transport forOut = Transit.inputTransport(is);
    System.out.println(forOut.toString());
    System.out.println();

I need change sources from fileoutput to System.out and fileinput for System.in and realize it in code. How can i do this? Transit.outputTransport(Parking, os) - writing byte string in file and Transit.inputTransport(is) - return object after translate bytes to normal form

Jay Castle
  • 49
  • 7
  • replace `is` with `System.in` and `os` with `System.out` – Lino Feb 06 '20 at 12:06
  • thank you for help, but it doesn`t work – Jay Castle Feb 06 '20 at 12:08
  • 1
    "it doesn't work", do you get errors? Do you see no output? Does it still write to a file? Could you please specify – Lino Feb 06 '20 at 12:09
  • Apps is running and waiting something on stage Transport forOut = Transit.inputTransport(System.in); what should i write here? – Jay Castle Feb 06 '20 at 12:11
  • 1
    Maybe you need to run the command with standard input / output redirected; e.g. `java MyMainClass < in.txt > out.txt`. – Stephen C Feb 06 '20 at 12:13
  • 1
    Note that your example seems to be reading AND writing `out.txt`. That is broken. 1) When you open a file for output (like that) it immediately truncates the file. 2) If you try to read and write a file at the same time, the results will be difficult to predict. – Stephen C Feb 06 '20 at 12:17
  • @JayCastle what happens when you write some text into the console. Seems that the app is waiting on input – Lino Feb 06 '20 at 12:18
  • @Lino Exception in thread "main" java.lang.NullPointerException at com.company.Main.main(Main.java:105) - there System.out.println(forOut.toString()); – Jay Castle Feb 06 '20 at 12:19
  • @StephenC but it works for my app and it`s all that i need know, but it doesn`t work for system.in and system.out – Jay Castle Feb 06 '20 at 12:20

1 Answers1

1
PrintStream os = new PrintStream(System.out);
Transit.outputTransport(Parking, os);
System.out.println(Parking.toString());
os.close();

InputStream is = new BufferedInputStream(System.in);
Transport forOut = Transit.inputTransport(is);
System.out.println(forOut.toString());
System.out.println();
is.close();
Arvind Kumar Avinash
  • 50,121
  • 5
  • 26
  • 72
  • it wont work, cuz inputTransport has InputStream param, not scanner – Jay Castle Feb 06 '20 at 13:56
  • @JayCastle - I've updated my answer. Feel free to comment if you still face any problem. – Arvind Kumar Avinash Feb 06 '20 at 14:58
  • Thank you, it works – Jay Castle Feb 06 '20 at 15:16
  • You are most welcome. – Arvind Kumar Avinash Feb 06 '20 at 15:17
  • Can u help with one more thing. What should i write in console for exit code 0? Console`s waiting some symbol or anythign else and when i enter something i have this error: null Exception in thread "main" java.lang.NullPointerException at com.company.Main.main(Main.java:246) – Jay Castle Feb 06 '20 at 15:25
  • And now i have another problem forOut - everytime null – Jay Castle Feb 06 '20 at 15:32
  • You may put `System.exit(0)` in your code based on the `null` check i.e. something like `if(something==null){System.exit(0);}` where `something` is the value to check for `null`. I can suggest more precisely if I see the complete code. I suggest you post a new question for this problem so that it can get more attention. – Arvind Kumar Avinash Feb 06 '20 at 15:33
  • I create new post https://stackoverflow.com/questions/60098656/fix-the-prolem-with-using-system-in-out-for-read-write-objects-in-by-file – Jay Castle Feb 06 '20 at 15:39
  • Alright. Let me check it. – Arvind Kumar Avinash Feb 06 '20 at 15:55
  • @JayCastle - I hope the solution worked for you. Do not forget to accept the answer so that future visitors can also use the solution confidently. Check https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work to learn how to do it. Feel free to comment in case of any doubt/issue. – Arvind Kumar Avinash May 27 '20 at 09:36