Java中InputStream写入到文件中

基于流(Stream)的解决
1.流是单向的有方向性的描述信息流的对象,InputStream是输入流的接口,对程序来说是入,是读,可以从文件读,缓存区读,网络节点读等等.
2.写入文件,对程序来说是出,是写,就是FileOutputStream,可以写入int也可以byte[]

所以解决方案就是从InputStream中读出内存到byte[]中然后,使用FileOutputStream写入文件中如:

//默认写入到本级目录
//FileOutputStream fos = new FileOutputStream("按钮事件控件整理.txt");
//写入到固定的目录
FileOutputStream fos = new FileOutputStream("/Users/11/Desktop/按钮/按钮事件控件整理1.txt", true);

//Unix下的换行符为"\n"			
fos.write("\n".getBytes());						
//Windows下的换行符为"\r\n"			
fos.write("\r\n".getBytes()); 						
//推荐使用,具有良好的跨平台性			
String newLine = System.getProperty("line.separator");			 
fos.write(newLine.getBytes());  

String line1 = "按钮名称: ".concat(sysButton.getButtonName());
fos.write(line1.getBytes());
fos.write("\n".getBytes());

byte[] b = new byte[1024];
int length;
while ((length = is.read(b)) > 0) {
    fos.write(b, 0, length);
}
is.close();
fos.close();// 保存数据

你可能感兴趣的:(java,开发语言)