11.FileInputStream和FileOutputStream
发表日期:2021-06-30 19:50:39 | 来源: | | 浏览(896) 分类:JAVA基础
InputStreamDemo
package stream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class InputStreamDemo {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File file = new File("D:" + File.separator + "123.txt");
FileInputStream inputStream = new FileInputStream(file);
/**/
byte[] b = new byte[1024];
int length = inputStream.read(b);//获取文件内容长度
System.out.println(new String(b));
int length2 = (int) file.length();//第二中方法(获取文件大小[字节数])
System.out.println(new String(b, 0, length2));
/*
System.out.println(file.length());
byte[] b2 = new byte[(int) file.length()];
int len = 0;
int temp = 0;
while ((temp=inputStream.read())!=-1) {
b2[len] = (byte) temp;
len++;
}
System.out.println(new String(b2,0,len));
*/
inputStream.close();
}
}OutputStreamDemo
package stream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class OutputStreamDemo {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File file = new File("D:"+File.separator+"OutputStreamDemo.txt");
FileOutputStream fOutputStream = new FileOutputStream(file,true);
String string ="true-追加写入模式";
fOutputStream.write(string.getBytes());
fOutputStream.close();
}
} - JAVA(0)
- JAVA基础(30)
- 1.JAVA开发环境配置(0)
- 2.java数据类型(0)
- 3.数组(0)
- 4.Date(0)
- 5.String 和 StringBuffer类常用方法(0)
- 6.Math类(0)
- 7.Cloneable(0)
- 8.File 文件(0)
- 9.FileReader和FileWriter(0)
- 10.RandomAccessFile(0)
- 11.FileInputStream和FileOutputStream(0)
- 12.InputStreamReader和OutputStreamWriter(0)
- 13.BufferedReader(0)
- 14.Scanner(0)
- 15.DataOutputStream(0)
- 16.Thead 多线程(0)
- 17.TimerTask(0)
- 18.zip(0)
- 19.Charset(0)
- 20.List(0)
- 21.Map(0)
- 22.Properties(0)
- 23.Enumeration(0)
- 24.Collection(0)
- 25.JDBC(0)
- 26.Iterator(0)
- 27.Abstract(0)
- 28.Interface(0)
- 29.Serializable(0)
- 30.Camparable(0)
- JSP基础(50)
宁公网安备 64010402001209号