IO流
文件路径
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class FileDemo {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
File f1 = new File("hello.java");
try {
f1.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File f2 = new File("abc/bac");
f2.mkdirs();
File f3 = new File("efg", "ior/java.txt");
f3.mkdirs();
System.out.println(f3.getPath());
System.out.println(f3.getParent());
System.out.println(f3.getName());
System.out.println(f3.getAbsolutePath());
System.out.println(f1.getAbsolutePath());
// try {
// f3.createNewFile();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
File f4 = new File(f3.getAbsolutePath(), "haha.java");
try {
f4.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// IO流
FileOutputStream out = new FileOutputStream("hello.java");
for (int i = 0; i < 5; i++) {
System.out.println("输入一串字符串:");
String str = sc.nextLine();
byte[] b = str.getBytes();
out.write(b);
}
out.close();
FileInputStream in = new FileInputStream("hello.java");
byte[] bs = new byte[6];
int len = 0;
while ((len = in.read(bs))!=-1) {
for (int i = 0; i < len; i++) {
System.out.print((char)bs[i]);
}
System.out.println();
}
in.close();
}
}
新特性
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class xintexingDemo {
public static void main(String[] args) {
File fl = new File("xintecxing.txt");
try(FileOutputStream out1 = new FileOutputStream("xintecxing.txt");
FileOutputStream out2 = new FileOutputStream("xintecxing.txt",true);) {
fl.createNewFile();
byte[] b1 =new byte[4];
String str1 = "nihao woshi shuaibi";
b1=str1.getBytes();
//追加
byte[] b2 =new byte[4];
String str2 = " shi me ni chou sha ";
b2=str2.getBytes();
out1.write(b1);
out2.write(b2);
out1.close();
out2.close();
} catch (Exception e) {
e.printStackTrace();
}finally {
}
}
}
序列化
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class xuliehua implements Serializable{
String name;
int age;
public xuliehua(String name,int age){
this.name = name;
this.age = age;
}
public static void main(String[] args) throws ClassNotFoundException {
// TODO Auto-generated method stub
xuliehua xl1 = new xuliehua("俊成",56);
xuliehua xl2 = new xuliehua("苏州",36);
// File fi = new File("xuliehua.txt");
// String str = "ni hao nis shi shei ";
// byte[] b = new byte[6];
// b = str.getBytes();
try (FileOutputStream out = new FileOutputStream("xuliehua.txt");
ObjectOutputStream oout = new ObjectOutputStream(out);
FileInputStream in = new FileInputStream("xuliehua.txt");
ObjectInputStream iin = new ObjectInputStream(in);){
oout.writeObject(xl1);
oout.writeObject(xl2);
xuliehua xl3 = (xuliehua)iin.readObject();
xuliehua xl4 = (xuliehua)iin.readObject();
System.out.println(xl3.name+"\t"+xl3.age);
System.out.println(xl4.name+"\t"+xl4.age);
} catch (IOException e) {
e.printStackTrace();
}
}
}
缓冲流
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class huanchongliu {
public static void main(String[] args) throws IOException {
File fl = new File("huanchongliu.txt");
String str = "这和上一个面试题有联系,有时面试官会用这个问题来评估你对泛型的理解,而不是直接问你什么是限定通配符和非限定通配符。这两个List的声明都是 限定通配符的例子,List extends T>可以接受任何继承自T的类型的List,而List super T>可以接受任何T的父类构成的List。例如List extends Number>可以接受List或List。在本段出现的连接中可以找到更多信息。";
byte[] b = str.getBytes();
FileOutputStream out = new FileOutputStream("huanchongliu.txt");
out.write(b);
out.close();
//新特性,直接生成文件并写入,没法追加
// try (
// PrintWriter printWriter = new PrintWriter("abc.txt", "utf-8");
// ) {
// printWriter.println("Hello 李某某2");
// } catch (Exception e) {
// e.printStackTrace();
// }
InputStream is = new FileInputStream("huanchongliu.txt");
InputStreamReader ir = new InputStreamReader(is, "GBK");
char[] value = new char[1024];
int len = 0;
while ((len = ir.read(value)) != -1) {
for (int i = 0; i < len; i++) {
char c = value[i];
System.out.print(c);
}
System.out.println();
}
System.out.println();
BufferedReader br = new BufferedReader(ir);
String value2 = null;
while ((value2 = br.readLine()) != null) {
System.out.println(value2);
}
}
}
propertise
mykey=myValue
name=java
address=\u5317\u4EAC
import java.io.InputStream;
import java.util.Properties;
public class TestProperties1 {
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
InputStream is = TestProperties1.class.getClassLoader().getResourceAsStream("mypropertirs.properties");
properties.load(is);
String myValue = properties.getProperty("mykey");
String name = properties.getProperty("name");
String address = properties.getProperty("address");
String error = properties.getProperty("error"); // 没有error这个key
String stuname = properties.getProperty("stuName");
System.out.println(myValue);// myValue
System.out.println(name);// java
System.out.println(address);// 北京
System.out.println(error);// null
System.out.println(stuname);
}
}
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
public class TestPrinterWriter1 {
public static void main(String[] args) {
try(
FileOutputStream fo = new FileOutputStream("oracle.txt",true);
// OutputStreamWriter ow = new OutputStreamWriter(fo, "utf-8");
PrintWriter printWriter = new PrintWriter(fo,true);//是否追加
){
printWriter.println("Hello");
}catch(Exception e){
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.net.URL;
import java.util.Properties;
public class TestProperties2 {
public static void main(String[] args) throws Exception {
update_properies("C:\\Users\\admin\\workspace\\FoundationStudies\\src\\myproperpies.properties", "school", "郑大");
update_properies("C:\\Users\\admin\\workspace\\FoundationStudies\\src\\myproperpies.properties", "school1", "henandaxue");
}
public static void update_properies(String path, String key, String value) {
File file = new File(path);
Properties prop = new Properties();
// InputStream inputFile = null;
// OutputStream outputFile = null;
try (InputStream inputFile = new FileInputStream(file);
// inputFile.close();//一定要在修改值之前关闭inputFile
OutputStream outputFile = new FileOutputStream(file,true);
// 设值-保存
) {
prop.load(inputFile);
prop.setProperty(key, value);
// 添加注释
prop.store(outputFile, "Update '" + key + "'+ '" + value);
} catch (IOException e) {
e.printStackTrace();
}
// try (Writer w = new PrintWriter("oracle.txt","utf-8");){
// w.write("hello 俊成");
//
// } catch (Exception e) {
// e.printStackTrace();
// }
}
}
图片
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class ImageDemo {
public static void main(String[] args) throws IOException {
File fi1 = new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\沙漠.jpg");
fi1.createNewFile();
FileInputStream fi = new FileInputStream("C:\\Users\\Public\\Pictures\\Sample Pictures\\沙漠.jpg");
FileOutputStream fo = new FileOutputStream("shamo.jpg");
byte[] bs = new byte[1024];
int len = 0;
while ((len = fi.read(bs)) != -1) {
for (int i = 0; i < len; i++) {
fo.write(bs);
}
System.out.println();
}
}
}
编码
import java.io.IOException;
import java.io.UnsupportedEncodingException;
public class bianma {
public static void main(String[] args) throws UnsupportedEncodingException {
String oname = "测试";
byte[] bs = oname.getBytes("GBK");
String dname = new String(bs, "GBK");
System.out.println(dname); // 测试
String oname1 = "测试";
byte[] bs1 = oname1.getBytes("GBK");
String dname1 = new String(bs1, "UTF-8");
System.out.println(dname1);// ????
String str = "测试";
byte[] bytes = str.getBytes("utf-8");
// 使用错误的字符编码解码
String errorStr = new String(bytes, "gbk");
System.out.println(errorStr);
// 使用错误的字符编码还原回byte[]
byte[] bytes2 = errorStr.getBytes("gbk");
// 再用正确的字符编码来解码,成功解决乱码问题
String rightStr = new String(bytes2, "utf-8");
System.out.println(rightStr);
}
}
NIO
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class NioDemo {
public static void main(String[] args) {
FileChannel inChannel = null;
FileChannel outChannel = null;
File file1 = new File("channel1.txt");
File file2 = new File("channel2.txt");
try {
// 创建FileInputStream,以该文件创建输入流创建FileChannel
inChannel = new FileInputStream(file1).getChannel();
ByteBuffer byteBuffer = inChannel.map(
FileChannel.MapMode.READ_ONLY, 0, file1.length());
outChannel = new FileOutputStream(file2).getChannel();
// 将byteBuffer里的数据全部输出
outChannel.write(byteBuffer);
} catch (IOException e) {
e.printStackTrace();
} finally {
//省略关闭流代码
}
}
}