输出流:
OutputStream()
import java.io.File; import java.io.OutputStream; import java.io.FileOutputStream; public class Hello{ public static void main(String args[]) throws Exception{ File file = new File("D:" + File.separator + "A_test" + File.separator + "my.txt"); if(!file.getParentFile().exists()){ file.getParentFile().mkdirs(); } OutputStream os = new FileOutputStream(file); String msg = "hello world, my is OutputStream()"; byte data[] = msg.getBytes(); os.write(data); os.close(); } }
输入流:
Inputstream()
import java.io.File; import java.io.FileInputStream; import java.io.InputStream; public class Hello{ public static void main(String args[]) throws Exception{ File file = new File("D:" + File.separator + "A_test" + File.separator + "my.txt"); if(file.exists() && file.isFile()){ InputStream input = new FileInputStream(file); byte data[] = new byte[1024] ; int len = input.read(data) ; System.out.println(new String(data,0,len)); } } }
时间: 2024-10-10 02:18:27