Java往文件写入字符串
代码如下:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Scanner;
public class text {
public static void main(String[] args) {
Scanner input1=new Scanner(System.in);
String a1=input1.next();
byte []a=a1.getBytes();
Scanner input2=new Scanner(System.in);
String b1=input2.nextLine();
byte []b=b1.getBytes();
File file=new File("D:\\java","a.txt");
try{
OutputStream w=new FileOutputStream(file);
System.out.println(file.getName()+"的大小:"+file.length());
w.write(a);
System.out.println(file.getName()+"的大小:"+file.length());
w.close();
w=new FileOutputStream(file,true);
w.write(b,0,b.length);
System.out.println(file.getName()+"的大小:"+file.length());
w.close();
}
catch(IOException e){
System.out.println("ERROR"+e);
}
}
}
运行结果: