File是一个 文件和目录路径名 的抽象表示,通过File可以查看文件的各种信息,也可以增加删除文件。
int available() 返回从该输入流中可以读取(或跳过)的字节数的估计值,而不会被下一次调用此输入流的方法阻塞。 void close() 关闭此输入流并释放与流相关联的任何系统资源。 void mark(int readlimit) 标记此输入流中的当前位置。 boolean markSupported() 测试这个输入流是否支持 mark和 reset方法。 abstract int read() 从输入流读取数据的下一个字节。 int read(byte[] b) 从输入流读取一些字节数,并将它们存储到缓冲区 b 。 int read(byte[] b, int off, int len) 从输入流读取最多 len字节的数据到一个字节数组,从数组off开始写入。 void reset() 将此流重新定位到上次在此输入流上调用 mark方法时的位置。 long skip(long n) 跳过并丢弃来自此输入流的 n字节数据。
FileDescriptor
)给构造器 ,从文件中读数据,找不到文件报异常。PipedInputStream
读入,此数据是由另一个线程写入到对应的PipedOutputStream中
。 单线程下使用可能还会造成死锁。int read(byte[] b) boolean readBoolean() byte readByte() char readChar() double readDouble() float readFloat() int readInt() long readLong() short readShort() String readUTF(string)
DataInputStream必须 DataOutputStream 搭配使用,才能正确得到结果。DataOutputStream 写入非字节数据时会附带数据长度信息,其他输入流 read 出来数据会出现乱码,正是因为这些长度信息使得他与平台无关,不管你在什么环境中DataOutputStream 写入,在什么环境中DataInputStream读出都不会错。
void close() 关闭此输出流并释放与此流相关联的任何系统资源。 void flush() 刷新此输出流并强制任何缓冲的输出。 void write(byte[] b) 将 b.length字节从指定的字节数组写入此输出流。 void write(byte[] b, int off, int len) 从指定的字节数组写入 len个字节,从偏移 off开始输出到此输出流。 abstract void write(int b) 将指定的字节写入此输出流。
toByteArray()可以返回已经写的字节数组
write(byte[] buf, int off, int len) 从一个字节数组中写入到流。write(int b)一次只写一个字节,也就是说只取b的低八位。
abstract void close() 关闭流并释放与之相关联的任何系统资源。 void mark(int readAheadLimit) 标记流中的当前位置。 boolean markSupported() 告诉这个流是否支持mark()操作。 int read() 读一个字符 int read(char[] cbuf) 将字符读入数组。 abstract int read(char[] cbuf, int off, int len) 将字符读入数组中的一部分。 int read(CharBuffer target) 尝试将字符读入指定的字符缓冲区。 boolean ready() 告诉这个流是否准备好被读取。 void reset() 重置流。 long skip(long n) 跳过字符
setLineNumber(int)
和getLineNumber()
用于分别设置和获得当前行号。没多大用处,建议不使用。Writer append(char c) 将指定的字符追加到后面。 Writer append(CharSequence csq) 将指定的字符序列追加到后面。 Writer append(CharSequence csq, int start, int end) 将指定字符序列的子序列追加到后面。 abstract void close() 关闭流, abstract void flush() 刷新流。 void write(char[] cbuf) 写入一个字符数组。 abstract void write(char[] cbuf, int off, int len) 写入字符数组的一部分。 void write(int c) 写一个字符 void write(String str) 写一个字符串 void write(String str, int off, int len) 写一个字符串的一部分。
connect(PipedReader snk) 与PipedReader关联起来
(Writer out, int sz)
newLine()
添加一行分隔符。RandomAccessFile 可以任意访问文件的某处,既可以读又可以写,功能比较强大。
RandomAccessFile(File file, String mode)
创建一个任意访问文件流从File参数指定的文件中读取,可设置文件权限。
RandomAccessFile(String name, String mode)
创建一个任意访问文件流,以从中指定名称的文件读取,可设置文件权限。
/* * 字符流读取文件 * 设置编码格式 * BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8")); */ BufferedReader in = new BufferedReader(new FileReader(file)); String s; StringBuilder sb = new StringBuilder(); while ((s = in.readLine()) != null){ sb.append(s+"\n"); } in.close(); System.out.println(sb); /* * 字节流读取文件 * 不使用缓冲区 * FileInputStream in = new FileInputStream(file); * */ //默认缓冲区大小8192字节=8M BufferedInputStream in = new BufferedInputStream(new FileInputStream(file)); byte[] bytes = new byte[1024]; //读到一个字节数组中,然后转换字符串时可以指定编码 // 如果使用read只读一个字节,单个字节没有编码,这样遇到中文无法处理 while (in.read(bytes) != -1){ System.out.print(new String(bytes,"UTF-8")); } in.close();
/*字节流只能写入字节*/ FileOutputStream out = new FileOutputStream(file2); BufferedOutputStream bo = new BufferedOutputStream(out); bo.write(98); bo.flush(); bo.close(); /*字符流可以写入 char string char[] */ BufferedWriter bw = new BufferedWriter(new FileWriter(file)); bw.write(85); bw.write("koko"); bw.flush(); bw.close(); //快速写入文件 PrintWriter p = new PrintWriter(file); p.write(9); p.write("koko"); p.flush(); p.close();
//打开或创建一个文件,赋予读写权限 RandomAccessFile accessFile = new RandomAccessFile(file3,"rw"); for (int i = 0; i < 5; i++) { accessFile.writeDouble(i*1.314); } accessFile.close(); accessFile = new RandomAccessFile(file3,"rw"); /*seek()以字节定位到某一位置 * 一个double8字节长,这里定位到第三个double开始 * 重新写入一个并覆盖 */ accessFile.seek(2*8); accessFile.writeDouble(8888D); //重新执行开始位置 accessFile.seek(0); for (int i = 0; i < 5; i++) { System.out.println(accessFile.readDouble()); } accessFile.close(); } /*output 0.0 1.314 8888.0 3.942 5.256 */
PrintStream p = System.out;// p 必须在重定向之前设定, PrintStream stream = new PrintStream(new BufferedOutputStream(new FileOutputStream(file4))); System.setOut(stream); for (int i = 0; i < 100; i++) { System.out.println("Spring 实战第"+(i+1)+"页"); } stream.close(); System.setOut(p);//不能使用System.setOut(System.out);这样是无效的 System.out.println("回来");
public abstract class Processextends Object;
ProcessBuilder.start()和Runtime.exec方法创建一个本机进程并返回一个Process子类的Process ,
可以用来控制进程并获取有关它的信息。
Process类提供了用于执行进程输入,
执行到进程的输出,等待进程完成,
检查进程的退出状态以及破坏(杀死)进程的方法
read(ByteBuffer[] dsts)
从该通道读取数据到缓冲区。write(ByteBuffer[] srcs)
从给定的缓冲区向该通道写入一系列字节。read(ByteBuffer[] dsts) 后应该立刻调用缓冲器的flip()它会重置缓冲器中的几个指针,让read之后的write从正确的地方开始和结束。
write(ByteBuffer[] srcs) 后应该立刻调用缓冲器的clean()方法将缓冲指针重置为初始态,为下一次read重写缓冲区做准备。
ByteBuffer buffer = ByteBuffer.allocate(1024); buffer.asIntBuffer().put(2018); int i = buffer.getInt();
ByteBuffer bb= ByteBuffer.allocate(1024); CharBuffer charBuffer = bb.asCharBuffer(); DoubleBuffer doubleBuffer = bb.asDoubleBuffer();
File file = new File("C:\\Users\\Administrator\\Desktop\\file\\com\\nio1.txt"); File path = new File("H:\\WinXP-2018.07.GHO"); PrintStream p = System.out; MappedByteBuffer map = new FileInputStream(path).getChannel().map(FileChannel.MapMode.READ_ONLY, 0, 0x8FFFFFF); //数据太大可以使用16进制 System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream(file)))); for (int i = 0; i < 0x8FFFFFF; i++) { System.out.println(map.get()); }
数据太大可以使用16进制数。这里只使用了读通道,把结果重定向到其他文件了。
最好使用RandomAccessFile来产生既可以读又可以写的通道。因为映射写必须要使用RandomAccessFile来产生写通道,不能使用FileOutputStream。
lock(long position, long size, boolean shared)
可以给文件部分加锁,shared为true则加共享锁,但这需要操作系统支持,不支持则加独占锁。BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8")); //BufferedOutputStream out = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(file2))); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new GZIPOutputStream(new FileOutputStream(file2)),"UTF-8")); int c; while ((c = in.read()) != -1){ out.write(c); } in.close(); out.flush(); out.close(); BufferedReader reader = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(file2)),"UTF-8")); String s; while ((s = reader.readLine()) != null){ System.out.println(s); } reader.close();
ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(file3)); BufferedOutputStream out = new BufferedOutputStream(zip); zip.putNextEntry(new ZipEntry(file)); BufferedReader in = new BufferedReader(new FileReader(file));
People people = new People(); people.setName("序列化到文件"); people.setAge(20); ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("./people.out")); out.writeObject(people); people.setName("序列化到流"); people.setAge(15); ByteArrayOutputStream bo = new ByteArrayOutputStream(); ObjectOutputStream oi = new ObjectOutputStream(bo); oi.writeObject(people); ObjectInputStream bytes = new ObjectInputStream(new ByteArrayInputStream(bo.toByteArray())); ObjectInputStream in = new ObjectInputStream(new FileInputStream("./people.out")); People p = (People) in.readObject(); System.out.println(p); p = (People)bytes.readObject(); System.out.println(p);
writeExternal(ObjectOutput out) 序列化时自动调用,可以将一些属性的信息通过out.writeXXX()保存
readExternal(ObjectInput in) 反序列化时自动调用,相当于构造器给某些属性赋值,可以通过in.readXXX()将保存的信息赋值给某些属性。
writeExternal 方法才可以保存。
private void writeObject(ObjectOutputStream stream) throws IOExpection
private void
readObject(ObjectInputStream stream
) throws IOExpection,ClassNotFoundException
public class SerializableDemo { public static void main(String[] args) throws Exception{ People people = new People(); people.setName("序列化到文件"); people.setAge(20); people.setSex("难"); ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("./people.out")); out.writeObject(people); ObjectInputStream in = new ObjectInputStream(new FileInputStream("./people.out")); People p = (People) in.readObject(); System.out.println(p); } } class People implements Serializable { String name; transient String sex; transient int age; Date date; private void writeObject(ObjectOutputStream stream) throws IOException{ stream.defaultWriteObject(); stream.writeObject(sex); stream.writeInt(age); } private void readObject(ObjectInputStream stream) throws IOException,ClassNotFoundException{ stream.defaultReadObject(); sex = (String) stream.readObject(); age = stream.readInt(); } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public People() { this.date = new Date(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "People{" + "name=‘" + name + ‘\‘‘ + ", sex=‘" + sex + ‘\‘‘ + ", age=" + age + ", date=" + date + ‘}‘; } }
public class SyStemSerDemo { public SyStemSerDemo() { } public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { List<Shape> shapes = new ArrayList<Shape>(); for (int i = 0; i < 10; i++) { shapes.add(Shape.randomFactory()); } for (int i = 0; i < 10; i++) { shapes.get(i).setColor(Shape.YELLOW); } List<Class<? extends Shape>> shapeTypes = new ArrayList<Class<? extends Shape>>(); shapeTypes.add(Circle.class); shapeTypes.add(Square.class); shapeTypes.add(Line.class); ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("./rest.lpk")); out.writeObject(shapeTypes); Line.writeStatic(out); out.writeObject(shapes); System.out.println(shapes); } } abstract class Shape implements Serializable { public static int RED = 1, BLUE = 2, YELLOW = 3; private int xPos, yPos, dimension; private static Random random = new Random(47); private static int count = 0; public abstract void setColor(int newcoloe); public abstract int getColor(); public Shape(int xPos, int yPos, int dimension) { this.xPos = xPos; this.yPos = yPos; this.dimension = dimension; } public String toString() { return getClass().hashCode() + "--" + getClass() + "color【" + getColor() + "】 xPos【" + xPos + "】 yPos【" + yPos + "】 dimension【" + dimension + "】\n"; } public static Shape randomFactory() { int xVal = random.nextInt(100); int yVal = random.nextInt(100); int dim = random.nextInt(100); switch (count++ % 3) { default: case 0: return new Circle(xVal, yVal, dim); case 1: return new Square(xVal, yVal, dim); case 2: return new Line(xVal, yVal, dim); } } } class Circle extends Shape { private static int color = RED; public Circle(int xVal, int yVal, int dim) { super(xVal, yVal, dim); } public void setColor(int newColor) { color = newColor; } public int getColor() { return color; } } class Square extends Shape { private static int color; public Square(int xVal, int yVal, int dim) { super(xVal, yVal, dim); color = RED; } public void setColor(int newColor) { color = newColor; } public int getColor() { return color; } } class Line extends Shape { private static int color = RED; public Line(int xVal, int yVal, int dim) { super(xVal, yVal, dim); } public void setColor(int newColor) { color = newColor; } public int getColor() { return color; } static void writeStatic(ObjectOutputStream stream) throws IOException { stream.writeInt(color); } static void readStatic(ObjectInputStream stream) throws IOException, ClassNotFoundException { color = stream.readInt(); } }
//用不同于序列化的类加载器反序列化,即 其他类中反序列化 public class Rests { public static void main(String[] args) throws IOException, ClassNotFoundException { ObjectInputStream in = new ObjectInputStream(new FileInputStream("./rest.lpk")); List<Class<? extends Shape>> shapeTypes = (List<Class<? extends Shape>>) in.readObject(); System.out.println(Line.class.hashCode()); System.out.println(shapeTypes.get(2).hashCode()); Line.readStatic(in); List<Shape> shapes = (List<Shape>) in.readObject(); System.out.println(shapes); } }
[621009875--class Circlecolor【3】 xPos【58】 yPos【55】 dimension【93】 , 1846274136--class Squarecolor【3】 xPos【61】 yPos【61】 dimension【29】 , 1360875712--class Linecolor【3】 xPos【68】 yPos【0】 dimension【22】 , 621009875--class Circlecolor【3】 xPos【7】 yPos【88】 dimension【28】 , 1846274136--class Squarecolor【3】 xPos【51】 yPos【89】 dimension【9】 , 1360875712--class Linecolor【3】 xPos【78】 yPos【98】 dimension【61】 , 621009875--class Circlecolor【3】 xPos【20】 yPos【58】 dimension【16】 , 1846274136--class Squarecolor【3】 xPos【40】 yPos【11】 dimension【22】 , 1360875712--class Linecolor【3】 xPos【4】 yPos【83】 dimension【6】 , 621009875--class Circlecolor【3】 xPos【75】 yPos【10】 dimension【42】 ]
2129789493 2129789493 [931919113--class Circlecolor【1】 xPos【58】 yPos【55】 dimension【93】 , 764977973--class Squarecolor【0】 xPos【61】 yPos【61】 dimension【29】 , 2129789493--class Linecolor【3】 xPos【68】 yPos【0】 dimension【22】 , 931919113--class Circlecolor【1】 xPos【7】 yPos【88】 dimension【28】 , 764977973--class Squarecolor【0】 xPos【51】 yPos【89】 dimension【9】 , 2129789493--class Linecolor【3】 xPos【78】 yPos【98】 dimension【61】 , 931919113--class Circlecolor【1】 xPos【20】 yPos【58】 dimension【16】 , 764977973--class Squarecolor【0】 xPos【40】 yPos【11】 dimension【22】 , 2129789493--class Linecolor【3】 xPos【4】 yPos【83】 dimension【6】 , 931919113--class Circlecolor【1】 xPos【75】 yPos【10】 dimension【42】 ]
import java.io.*; import java.util.ArrayList; import java.util.List; import java.util.Random; public class SyStemSerDemo { public SyStemSerDemo() { } public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { List<Shape> shapes = new ArrayList<Shape>(); for (int i = 0; i < 10; i++) { shapes.add(Shape.randomFactory()); } for (int i = 0; i < 10; i++) { shapes.get(i).setColor(Shape.YELLOW); } ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("./rest.lpk")); Circle.writeStatic(out); Square.writeStatic(out); Line.writeStatic(out); out.writeObject(shapes); System.out.println(shapes); } } abstract class Shape implements Serializable { public static int RED = 1, BLUE = 2, YELLOW = 3; private int xPos, yPos, dimension; private static Random random = new Random(47); private static int count = 0; public abstract void setColor(int newcoloe); public abstract int getColor(); public Shape(int xPos, int yPos, int dimension) { this.xPos = xPos; this.yPos = yPos; this.dimension = dimension; } public String toString() { return getClass().hashCode() + "--" + getClass() + "color【" + getColor() + "】 xPos【" + xPos + "】 yPos【" + yPos + "】 dimension【" + dimension + "】\n"; } public static Shape randomFactory() { int xVal = random.nextInt(100); int yVal = random.nextInt(100); int dim = random.nextInt(100); switch (count++ % 3) { default: case 0: return new Circle(xVal, yVal, dim); case 1: return new Square(xVal, yVal, dim); case 2: return new Line(xVal, yVal, dim); } } } class Circle extends Shape { private static int color = RED; public Circle(int xVal, int yVal, int dim) { super(xVal, yVal, dim); } public void setColor(int newColor) { color = newColor; } public int getColor() { return color; } static void writeStatic(ObjectOutputStream stream) throws IOException { stream.writeInt(color); } static void readStatic(ObjectInputStream stream) throws IOException, ClassNotFoundException { color = stream.readInt(); } } class Square extends Shape { private static int color; public Square(int xVal, int yVal, int dim) { super(xVal, yVal, dim); color = RED; } public void setColor(int newColor) { color = newColor; } public int getColor() { return color; } static void writeStatic(ObjectOutputStream stream) throws IOException { stream.writeInt(color); } static void readStatic(ObjectInputStream stream) throws IOException, ClassNotFoundException { color = stream.readInt(); } } class Line extends Shape { private static int color = RED; public Line(int xVal, int yVal, int dim) { super(xVal, yVal, dim); } public void setColor(int newColor) { color = newColor; } public int getColor() { return color; } static void writeStatic(ObjectOutputStream stream) throws IOException { stream.writeInt(color); } static void readStatic(ObjectInputStream stream) throws IOException, ClassNotFoundException { color = stream.readInt(); } }
public final class System final类不能被实例化
原文:https://www.cnblogs.com/mibloom/p/9496019.html