随机访问流and合并流and序列化流andPropertoes

来源:互联网 发布:淘宝描述不符怎么处理 编辑:程序博客网 时间:2024/06/06 20:00
class RandomAccessTest {// 多线程 下载文件  随机访问流public static void main(String[] args) {RandomAccessFile f = null;try{// String name,String mode 模式(读写)f =new RandomAccessFile("D:\\xx.txt","rw");long pointer =f.getFilePointer();f.write("牧佑叔叔_");pointer +=",牧佑叔叔".getBytes();f.seek(pointer - "叔叔".getBytes.length);f.seek(1);//操作指针。从哪开始插数据0,1,2,3字节 // 所以转换//f.seek("牧".getBytes().length());f.wirte("么么哒");}catch(IOException e){} finally{if(f !=null){try{f.close();}catch(IOException ex){}}}}}
class SequenceInputStream {//  合并流。将两个文件合并到一个文件public static void main(String[] args) thorws IoException{//String流ByteArrayInputStream bis = new ByteArrayInputStream("牧佑".getBytes());ByteArrayInputStream bis2 = new ByteArrayInputStream("叔叔".getBytes());SequenceInputStream sis = new SequenceInputStream(bis,bis2);StringBuilder sb = new StringBuilder();//放到byte数组里面byte[] buf = new byte[1024];int len = -1;while((len =sis.reder(buf))!= -1){byte[] tm = new byte[len];//System.arraycopy(buf,0,tm,0,len);sb.append(new String(buf,0,len));}System.out.println(sb.toString());}}






class ObjectOutputStream and ObjectInputStream {// 序列化流 操作对象流化  public static void main(String[] args)  throws FileNotFoundException{FileOutputStream fos = new FileOutputStream("D:\\xx.txt");// 对象必须继承序列化接口//如果对象字段不需要 系列化 使用 transient声明即可// private transient String name;Student s = new Student();s.setMomo("腹股沟");s.setAff("好像傻");ObjectOutputStream oos = new ObjectOutputStream(fos);oos.writerObject(s);oos.flush();oos.close();FileInputStream f = new FileInputStream("D:\\xx.txt");ObjectInputStream i = new ObjectInputStream(f);Student sfg = (Student)i.readObject();System.err.println(sfg.getMomo);i.close();}}






class PropertiesDemo {// Propertoes 与 IO 结合public static void main(String[] args)  throws IOException{Reader r = new Reader("D:\\xt.properties");// 新IO 可以用pathsPath path = Paths.get("d:","pro","sd.txt");Properties pro = new Properties();pro.load(r);pro.close();Enumeration<String>name = (Enumeration<String>)pro.propertyName();while(name.hasMoreElements){String key = name.nextElements()'Sytem.out.println("key:"+key);System.out.println("values:"+pro.getProperty(key));//读Writer e = new FileWriter("D:\\xt.properties");pro.setProperty("student","牧佑");pro.store(e,"新增");e.close();}}}


阅读全文
0 0
原创粉丝点击