Home > Back-end >  For help
For help

Time:12-16

Using the input/output flow programming knowledge: the string "welcome you!" Appended to "D: \ Java directory bianchengti. TXT file at the end, and then read the file, display the content of the text on the screen,


CodePudding user response:

Is it not an InputStream and OutputStream problem?

CodePudding user response:

 
Public static void main (String [] args) {
Scanner scanner=new Scanner(System.in);
System. The out. Println (" please input string ");
String input=scanner. The next ();
FileInputStream fis=null;
The File File=new File (" D:/test. TXT ");
Try {
Byte [] buf=new byte [16].
Int length=0;
Fis=new FileInputStream (file);
The StringBuilder StringBuilder=new StringBuilder ();
While ((length=fis. Read (buf))!=1) {
StringBuilder. Append (new String (buf, 0, length));
}

System. The out. Println (stringBuilder. ToString ());

The stringBuilder. Append (input);

FileOutputStream fos=new FileOutputStream (file);
Fos. Write (stringBuilder. ToString () getBytes ());
Fis. Close ();
{} the catch (FileNotFoundException fnfe)
System. The out. Println (fnfe);
} the catch (IOException ioe) {
System. The out. Println (ioe);
}
}
  • Related