当前位置: 首页 > news >正文

做商城网站建设哪家好wordpress 绑定两个域名

做商城网站建设哪家好,wordpress 绑定两个域名,wordpress 前台写文章,企业没有专业人员怎么建设网站IO流 凡是与输入#xff0c;输出相关的类#xff0c;接口等都定义在java.io包下 1.File类的使用 File类可以有构造器创建其对象#xff0c;此对象对应着一个文件(.txt,.avi,.doc,.mp3等)或文件目录 File类对象是与平台无关的 File中的方法仅涉及到如何创建#xff0c;…IO流 凡是与输入输出相关的类接口等都定义在java.io包下 1.File类的使用 File类可以有构造器创建其对象此对象对应着一个文件(.txt,.avi,.doc,.mp3等)或文件目录 File类对象是与平台无关的 File中的方法仅涉及到如何创建删除重命名等操作不涉及文件内容的修改(需IO流来操作) File类对象常作为io流的具体类的构造器的形参 常见的方法 熟练掌握红色标记的方法 例 import java.io.File; import java.sql.Date;import org.junit.Test; public class test10{Testpublic void test1(){//绝对路径File f1 new File(C:/Users/Cat God 007/Desktop/hello.txt); //文件File f2 new File(C:/Users/Cat God 007/Desktop);//文件目录//相对路径File f3 new File(hello.txt);System.out.println(访问文件名);System.out.println(f3.getName());//返回文件名 System.out.println(f3.getPath());//返回文件路径System.out.println(f3.getAbsoluteFile());//返回文件的绝对路径System.out.println(f3.getParent());//返回上一级文件目录System.out.println(f3.getAbsolutePath());//返回完整的文件路径System.out.println();System.out.println(f2.getName());//返回文件目录名System.out.println(f2.getPath());//返回文件目录路径System.out.println(f2.getAbsoluteFile());//返回文件目录的绝对路径System.out.println(f2.getParent());//返回上一级文件目录System.out.println(f2.getAbsolutePath());//返回完整的文件目录路径System.out.println(文件检测);System.out.println(f1.exists());//检测文件是否存在System.out.println(f1.canRead());//检测文件是否可读System.out.println(f1.canWrite());//检测文件是否可写System.out.println(f1.isFile());//检测此对象是否不是文件System.out.println(f1.isDirectory());//检测此对象是否不是文件目录System.out.println(获取常规文件信息);System.out.println(new Date(f1.lastModified()));//获取文件最后修改时间System.out.println(f1.length());//获取文件大小}Testpublic void test2(){File f1 new File(C:/Users/Cat God 007/Desktop/hello.txt); File f2 new File(C:/Users/Cat God 007/Desktop/test/tes1-test9);System.out.println(f1.delete());//删除文件if(!f1.exists()){boolean b1 f1.createNewFile();//创建文件System.out.println(b1);}if(!f2.exists()){boolean b2 f2.mkdir();//mkdirs()可以递归创建文件夹mkdir只创建最后的文件目录若它上层没有创建则它也不会创建System.out.println(b2);}File f3 new File(C:\\Users\\Cat God 007\\Desktop\\javacode\\day13);String[] list f3.list();for(int i 0;i list.length;i){System.out.println(list[i]);//以String方式读取出f3下的文件}File[] files f3.listFiles();for(int i 0;i files.length;i){System.out.println(files[i].getName());//以文件方式读取出f3下的文件}} }2.IO流原理及其分类 IO流用来处理设备之间的数据传输按数据单位可分为字节流(8bit)字符流(16bit)按流的流向可分为输入流输出流按流的角色可分为节点流(直接作为于文件)处理流 抽象基类字节流字符流输入流InputStreamReader输出流OutputStreamWriter IO流设计40多个类都是从以上4个抽象基类派生由这四个类派生出的子类名称都是以其父类名作为子类名后缀IO流体系 访问文件的类也被称为节点流文件流其他类被称为处理流 3.文件流 FileInputStream 例 import java.io.File; import java.io.FileInputStream; import java.io.IOException; import org.junit.Test;public class test11{Test //读取硬盘文件内容到程序中使用FileInputStream(读取的文件一定要存在否则会报文件找不到的异常) public void test1()throws Exception{//1.创建Filen类型的对象File file new File(hello.txt);//2.创建FileInputStream类型的对象FileInputStream files new FileInputStream(file); //3.调用FileInputStream的方法,实现file文件的读取//read()读取文件的一个字节当执行到文件结尾时返回-1//方式一int b files.read();while(b ! -1){System.out.println((char)b);//int转字符(否则打印出来的是对应的Ascll码)b files.read();}//方式二int c;while((c files.read()) ! -1){System.out.println((char)c);}//4.关闭相应的流files.close(); }Test//测试test1的优化确保每次流都能被关闭public void test2(){FileInputStream files null;try {File file new File(hello.txt);files new FileInputStream(file);int c;while((c files.read()) ! -1){System.out.println((char)c);}} catch (IOException e) {e.printStackTrace();}finally{if(files ! null){try{files.close();}catch(IOException e){e.printStackTrace();}} } }Testpublic void test3(){FileInputStream files null;try{File file new File(hello.txt);files new FileInputStream(file);byte[] b new byte[5];//将读取到的数据写入数组中int len;//每次读入到byte中的字节长度while((len files.read(b)) ! -1){// 方式一:运行for循环实现遍历输出// 成功案例:for(int i 0;i len;i){System.out.print((char)b[i]);}//错误案例使用b.length时在读取到最后此时只读取了1个元素但依旧会传入5个元素的数组{1个新元素4个旧元素}// for(int i 0;i b.length;i){// System.out.print((char)b[i]);// }//方式二运行String构造器实现遍历输出// String str new String(b, 0, len);// System.out.println(str);}}catch(IOException e){e.printStackTrace();}finally{if(files ! null){try{files.close();}catch(IOException e){e.printStackTrace();}}}} }FileOutputStream 简单编写 import java.io.File; import java.io.FileOutputStream; import org.junit.Test;public class test12{Testpublic void testFileOutputSteam(){//1.创建要写入文件的文件路径的File对象此文件路径可以不存在会自动创建若存在就会用新写入的数据覆盖原来的数据File file new File(hello1.txt);//2.创建FileOutputStream对象将之前的File对象作形参传入FileOutputStream的构造器中FileOutputStream f null;try{ f new FileOutputStream(file);//3.写入数据f.write(new String(I Love China).getBytes());//这里用到了字符串转字节数组}catch(Exception e){e.printStackTrace();}finally{//关闭输出流if(f ! null){try{f.close();}catch(Exception e){e.printStackTrace();}}}} }练习编写非文本文件复制的方法 主要实现 import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import org.junit.Test;public class test12{Test//从硬盘中读入数据将此数据写入到另一位置相当文件复制)public void testFileInputOutputSteam(){//1.提供读写的文件路径File file1 new File(C:\\Users\\Cat God 007\\Desktop\\t1.jpg);File file2 new File(C:\\\\Users\\\\Cat God 007\\\\Desktop\\\\tt.jpg);//2.提供读写流FileOutputStream fos null;FileInputStream fis null;try{ fis new FileInputStream(file1);fos new FileOutputStream(file2);//3.实现文件复制byte [] b new byte[20];int len;while ((len fis.read(b)) ! -1) {fos.write(b,0,len); }}catch(Exception e){e.printStackTrace();}finally{//4.关闭读,写流if(fos ! null){try{fos.close();}catch(Exception e){e.printStackTrace();}}if(fis ! null){try{fis.close();}catch(Exception e){e.printStackTrace();}}}}最后包装成方法并进行测试 import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream;public class test12{public static void main(String[] args) { testFileInputOutputSteam(C:\\Users\\Cat God 007\\Desktop\\t1.jpg, C:\\Users\\Cat God 007\\Desktop\\tt1.jpg);}public static void testFileInputOutputSteam(String src,String dest){File file1 new File(src);File file2 new File(dest);FileOutputStream fos null;FileInputStream fis null;try{fis new FileInputStream(file1);fos new FileOutputStream(file2);byte [] b new byte[20];int len;while ((len fis.read(b)) ! -1) {fos.write(b,0,len); }}catch(Exception e){e.printStackTrace();}finally{if(fos ! null){try{fos.close();}catch(Exception e){e.printStackTrace();}}if(fis ! null){try{fis.close();}catch(Exception e){e.printStackTrace();}}}}FileReaderFileWriter字符流 import java.io.File; import java.io.FileReader; import java.io.FileWriter; import org.junit.Test;public class test13{//使用FileReaderFileWriter可以实现文本文件的复制Testpublic void testFileReaderWriter(){ FileReader fr null;FileWriter fw null; try{File src new File(hello.txt);//读File desc new File(hello1.txt);//写fr new FileReader(src);fw new FileWriter(desc);char[] c new char[20];int len;while ((len fr.read(c)) ! -1) {fw.write(c,0,len); }}catch(Exception e){e.printStackTrace();}finally{if(fr ! null){try{fr.close();}catch(Exception e){e.printStackTrace();}}if(fw ! null){try{fw.close();}catch(Exception e){e.printStackTrace();}}} } }文本文件用字符流非文本文件(视频文件,音频文件,图片)用字节流效率较高 4.缓冲流主要使用 可以提高处理数据的效率 每次处理完后都需要刷新flush())数组方便下次元素不够写入的情况 使用 BufferedInputStreamBufferedOutputStream 实现非文本文件的复制 例 import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import org.junit.Test;public class test14{Testpublic void testBufferedInputOutputStream(){BufferedInputStream bis null;BufferedOutputStream bos null;try{ //1.提供读写文件File file1 new File(C:\\Users\\Cat God 007\\Desktop\\t1.jpg);File file2 new File(.\\1.jpg);//2.创建相应的节点流FileInputStream fis new FileInputStream(file1);FileOutputStream fos new FileOutputStream(file2);//3.将创建的节点流的对象作为形参传递给缓冲流的构造器中bis new BufferedInputStream(fis);bos new BufferedOutputStream(fos);//4.实现文本文件byte[] b new byte[1024];int len;while ((len bis.read(b)) ! -1) {bos.write(b, 0, len);bos.flush();//刷新一下}}catch(Exception e){e.printStackTrace();}finally{//5.关闭相应的流if(bis ! null){try{bis.close();}catch(Exception e){e.printStackTrace();}}if(bos ! null){try{bos.close();}catch(Exception e){e.printStackTrace();}}} } } }使用BufferedReaderBufferedWriter实现文本文件的复制 import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import org.junit.Test;public class test{ // BufferedReader()的readLine()方法一行一行读 import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import org.junit.Test;public class test14{Testpublic void testBufferedReader(){BufferedReader br null;BufferedWriter bw null;try{ File file new File(hello.txt);File file1 new File(hell1o.txt);FileReader fr new FileReader(file);FileWriter fw new FileWriter(file1);br new BufferedReader(fr);bw new BufferedWriter(fw);// char[] c new char[1024];// int len;// while ((len br.read(c)) ! -1) {// String str new String(c, 0, len);// System.out.print(str);// }String str;while ((str br.readLine()) ! null) {//System.out.println(str);bw.write(str \n);//bw.newLine();//换行bw.flush();}}catch(Exception e){e.printStackTrace();}finally{if(bw ! null){try{bw.close();}catch(Exception e){e.printStackTrace();}}if(br ! null){try{br.close();}catch(Exception e){e.printStackTrace();}}}} }5.转换流 提供在字节流和字符流之间的转换 字节流中的数据都是字符时转换成字符流操作更高效 编码字符串字节数组解码字节数组字符串 提供两个转换流InputStreamReader解码OutputStreamWriter编码 例 import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter;import org.junit.Test;public class test16{Testpublic void test1(){BufferedReader br null;BufferedWriter bw null;try{ //解码字节数组字符串File file new File(hello.txt);FileInputStream fis new FileInputStream(file);InputStreamReader isr new InputStreamReader(fis, UTF-8);br new BufferedReader(isr);//编码字符串字节数组File file1 new File(hello123.txt);FileOutputStream fos new FileOutputStream(file1);OutputStreamWriter isw new OutputStreamWriter(fos, UTF-8);bw new BufferedWriter(isw);String str;while ((str br.readLine()) ! null) {//System.out.println(str);isw.write(str \n);//bw.newLine();//换行isw.flush();}}catch(Exception e){e.printStackTrace();}finally{if(bw ! null){try{bw.close();}catch(Exception e){e.printStackTrace();}}if(br ! null){try{br.close();}catch(Exception e){e.printStackTrace();}}}} }6.标准的输入输出流 标准的输出流System.out标准的输入流System.in 例 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader;import org.junit.Test;public class test16{Testpublic void test(){BufferedReader br null;try{ InputStream is System.in;//接受传入的字节流InputStreamReader isr new InputStreamReader(is); //字节转字符br new BufferedReader(isr); //包装成带缓冲的字符流String str;while(true){System.out.println(请输入字符串);str br.readLine();//忽略大小写if(str.equalsIgnoreCase(e) || str.equalsIgnoreCase(exit)){break;}String str1 str.toUpperCase();//转化成大写System.out.println(str1);}}catch(IOException e){e.printStackTrace();}finally{if(br ! null){try{br.close();}catch(IOException e){e.printStackTrace();}}}} }练习 在一个目录下创建test.txt文件,并写入以下内容 云计算是一个比较庞大的概念入门云计算首先要从掌握基本概念和基础知识开始然后通过掌握完全面向云计算的特定供应商的平台或技术等重要领域来增强其专业知识水平 这样可以更快的学好云计算。你需要学习这些知识 计算机与网络的基础知识 安全基础知识 编程语言基础 脚本语言 linux基础知识 分布式系统读取test.txt文件的内容并打印出来 复制test.txt文件为cloudcompute.txt文件 import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException;import org.junit.Test;public class test17{Test//字节流写入文本文件数据public void test1(){BufferedOutputStream bos null;try{ File file new File(C:\\Users\\Cat God 007\\Desktop\\test.txt);FileOutputStream fos new FileOutputStream(file);bos new BufferedOutputStream(fos);String str \u4E91\u8BA1\u7B97\u662F\u4E00\u4E2A\u6BD4\u8F83\u5E9E\u5927\u7684\u6982\u5FF5\uFF0C\u5165\u95E8\u4E91\u8BA1\u7B97\uFF0C\u9996\u5148\u8981\u4ECE\u638C\u63E1\u57FA\u672C\u6982\u5FF5\u548C\u57FA\u7840\u77E5\u8BC6\u5F00\u59CB\uFF0C\u7136\u540E\u901A\u8FC7\u638C\u63E1\u5B8C\u5168\u9762\u5411\u4E91\u8BA1\u7B97\u7684\u7279\u5B9A\u4F9B\u5E94\u5546\u7684\u5E73\u53F0\u6216\u6280\u672F\u7B49\u91CD\u8981\u9886\u57DF\u6765\u589E\u5F3A\u5176\u4E13\u4E1A\u77E5\u8BC6\u6C34\u5E73\uFF0C\r\n //\u8FD9\u6837\u53EF\u4EE5\u66F4\u5FEB\u7684\u5B66\u597D\u4E91\u8BA1\u7B97\u3002\u4F60\u9700\u8981\u5B66\u4E60\u8FD9\u4E9B\u77E5\u8BC6\uFF1A\r\n //\u8BA1\u7B97\u673A\u4E0E\u7F51\u7EDC\u7684\u57FA\u7840\u77E5\u8BC6\r\n //\u5B89\u5168\u57FA\u7840\u77E5\u8BC6\r\n //\u7F16\u7A0B\u8BED\u8A00\u57FA\u7840\r\n //\u811A\u672C\u8BED\u8A00\r\n //linux\u57FA\u7840\u77E5\u8BC6\r\n //\u5206\u5E03\u5F0F\u7CFB\u7EDF;bos.write(str.getBytes());}catch(IOException e){e.printStackTrace();}finally{if(bos ! null){try{bos.flush();}catch(Exception e){e.printStackTrace();}}}}Test//字符流写入文本文件数据public void test2(){BufferedWriter bw null;try{bw new BufferedWriter(new FileWriter(new File(C:\\Users\\Cat God 007\\Desktop\\test.txt)));String str \u4E91\u8BA1\u7B97\u662F\u4E00\u4E2A\u6BD4\u8F83\u5E9E\u5927\u7684\u6982\u5FF5\uFF0C\u5165\u95E8\u4E91\u8BA1\u7B97\uFF0C\u9996\u5148\u8981\u4ECE\u638C\u63E1\u57FA\u672C\u6982\u5FF5\u548C\u57FA\u7840\u77E5\u8BC6\u5F00\u59CB\uFF0C\u7136\u540E\u901A\u8FC7\u638C\u63E1\u5B8C\u5168\u9762\u5411\u4E91\u8BA1\u7B97\u7684\u7279\u5B9A\u4F9B\u5E94\u5546\u7684\u5E73\u53F0\u6216\u6280\u672F\u7B49\u91CD\u8981\u9886\u57DF\u6765\u589E\u5F3A\u5176\u4E13\u4E1A\u77E5\u8BC6\u6C34\u5E73\uFF0C\r\n //\u8FD9\u6837\u53EF\u4EE5\u66F4\u5FEB\u7684\u5B66\u597D\u4E91\u8BA1\u7B97\u3002\u4F60\u9700\u8981\u5B66\u4E60\u8FD9\u4E9B\u77E5\u8BC6\uFF1A\r\n //\u8BA1\u7B97\u673A\u4E0E\u7F51\u7EDC\u7684\u57FA\u7840\u77E5\u8BC6\r\n //\u5B89\u5168\u57FA\u7840\u77E5\u8BC6\r\n //\u7F16\u7A0B\u8BED\u8A00\u57FA\u7840\r\n //\u811A\u672C\u8BED\u8A00\r\n //linux\u57FA\u7840\u77E5\u8BC6\r\n //\u5206\u5E03\u5F0Fw\u7CFB\u7EDF;bw.write(str);}catch(IOException e){e.printStackTrace();}finally{if(bw ! null){try{bw.flush();}catch(Exception e){e.printStackTrace();}}}}Test//字符流输出文本文件数据public void test3(){BufferedReader br null;try{br new BufferedReader(new FileReader(C:\\Users\\Cat God 007\\Desktop\\test.txt));String str;while((str br.readLine()) ! null){System.out.println(str);}}catch(IOException e){e.printStackTrace();}finally{if(br ! null){try{br.close();}catch(IOException e){e.printStackTrace();}}}}Test//字符流实现复制public void test4(){BufferedWriter bw null;BufferedReader br null;try{br new BufferedReader(new FileReader(C:\\Users\\Cat God 007\\Desktop\\test.txt));bw new BufferedWriter(new FileWriter(C:\\Users\\Cat God 007\\Desktop\\cloudcompute.txt));String str;while((str br.readLine()) ! null){bw.write(str \n);} }catch(IOException e){e.printStackTrace();}finally{if(bw ! null){try{bw.close();}catch(IOException e){e.printStackTrace();}}if(br ! null){try{br.close();}catch(IOException e){e.printStackTrace();}}}}}7.打印流 PrintStream字节流 printWriter字符流 例 public class test18{Test//打印流public void printStreamTest(){FileOutputStream fos null;try{ fos new FileOutputStream(C:\\Users\\Cat God 007\\Desktop\\test.txt);}catch(IOException e){e.printStackTrace();}//创建打印输出流设置为自动刷新模式写入换行符就或字节\n时都会刷新输出缓冲区PrintStream ps new PrintStream(fos,true);if(ps ! null){//把标准输出流改成文件System.setOut(ps);//修改输出打印的位置为ps}for(int i 0;i 255;i){System.out.print((char)i);if(i % 50 0){//每50数据一行System.out.println();//换行}}ps.close();} }8.数据流 用来处理基本数据类型String,字节数组的数据 例 import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException;import org.junit.Test;public class test19{Test//数据流写入public void testData(){DataOutputStream dos null;try { FileOutputStream fos new FileOutputStream(C:\\Users\\Cat God 007\\Desktop\\test.txt);dos new DataOutputStream(fos);dos.writeUTF(你好);dos.writeInt(467876543);dos.writeDouble(1289.789);dos.writeBoolean(true);}catch(IOException e){e.printStackTrace();}finally{if(dos ! null){try {dos.close();} catch (IOException e){e.printStackTrace();}}}}Test//数据流读取public void testData1(){DataInputStream dis null;try { dis new DataInputStream(new FileInputStream(C:\\Users\\Cat God 007\\Desktop\\test.txt));String str dis.readUTF();System.out.println(str);int i dis.readInt();System.out.println(i);double d dis.readDouble();System.out.println(d);boolean b dis.readBoolean();System.out.println(b);} catch (IOException e) {e.printStackTrace();}finally{if(dis ! null){try {dis.close();} catch (IOException e){e.printStackTrace();}}}} }9.对象流 ObjectOutputStream 和 ObjectInputStream 用于存储和读取对象的处理流可以把Java中的对象写入到数据源中也能把对象从数据源中还原回来 序列化Serialize用 ObjectOutputStream 类将Java对象写入IO流中 反序列化Deserialize用 ObjectInputStream 类从IO流中恢复该Java对象 ObjectOutputStream 和 ObjectInputStream不能序列化static和transient短暂的修饰的成员变量 例 i mport java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import org.junit.Test;public class test20{Test// 反序列化--ObjectInputStreampublic void testObjectInputStream(){ObjectInputStream ois null;try { ois new ObjectInputStream(new FileInputStream(C:\\\\Users\\\\Cat God 007\\\\Desktop\\\\test.txt));Person p1 (Person)ois.readObject();//读取一个对象System.out.println(p1.toString());//打印一个对象Person p2 (Person) ois.readObject();//读取对象System.out.println(p2);//打印对象}catch (IOException e){e.printStackTrace();}finally{if(ois ! null){try{ois.close();}catch (IOException e){e.printStackTrace();}}}}Test//对象序列化---ObjectOutputStreampublic void testObjectOutputStream(){Person p1 new Person(张三, 20,new Pet(张二));Person p2 new Person(李四, 21,new Pet(李三));ObjectOutputStream oos null;try{oos new ObjectOutputStream(new FileOutputStream(C:\\\\Users\\\\Cat God 007\\\\Desktop\\\\test.txt));oos.writeObject(p1);oos.flush();oos.writeObject(p2);oos.flush(); }catch(Exception e){e.printStackTrace();}finally{if(oos ! null){try {oos.close();} catch (IOException e) {e.printStackTrace();}}}} } /** 实现序列化的类* 1.此类可序列化需要此类实现Serializable接口* 2.此类可序列化需要类的属性同样实现Serializable接口* 3.版本号凡是实现Serializable接口的类都有一个表示序列化版本标识符的静态变量表明类的不同版本间的兼容性如果没有定义那在运行时会自动生成如果源码修改版本号自动生成的可能会变化* 如private static final long serialVersionUID 1234678876543L; */ class Person implements Serializable{ private static final long serialVersionUID 12343128876543L; String name;Integer age;Pet pet;public Person(String name, Integer age,Pet pet) { this.name name;this.age age; this.pet pet;}Overridepublic String toString() { return Person{ name name \ , age age \ , pet pet \ };} } class Pet implements Serializable{String name;public Pet(String name) {this.name name;} }10.随机存取文件流 RandomAccessFile类支持随机访问的方式程序可以直接跳到文件的任意地方来读写文件 可以向已存在的文件后追加内容 支持只访问文件的部分内容 RandomAccessFile对象包含一个记录指针用于标示当前读写处的位置。RandomAccessFile类类对象可以自由移动记录指针 long getFilePointer()获取文件记录指针的当前位置 void seek(long pos)将文件记录指针定位到pos位置 例 import java.io.RandomAccessFile;import org.junit.Test;public class test21{Test//进行文件的读写public void test(){RandomAccessFile raf1 null;RandomAccessFile raf2 null;try{ raf1 new RandomAccessFile(hello.txt,r);raf2 new RandomAccessFile(hello1.txt,rw);byte[] b new byte[4];int len;while((len raf1.read(b))!-1){raf2.write(b,0,len);}}catch(Exception e){{e.printStackTrace();}}finally{if(raf2 ! null){try{raf2.close();}catch(Exception e){e.printStackTrace();}}if(raf1 ! null){try{raf1.close();}catch(Exception e){e.printStackTrace();}}}}Test//在指定位置进行文件的读写,实际上实现的是覆盖的效果public void test1(){RandomAccessFile raf null;try{raf new RandomAccessFile(hello.txt,rw);raf.seek(3);//把指针调到第三个的位置raf.write(123456789.getBytes());}catch(Exception e) {e.printStackTrace();}finally{if(raf ! null){try{raf.close();}catch(Exception e){e.printStackTrace();}}}}Test//实现在指定位置的插入效果public void test2(){RandomAccessFile raf null;try{raf new RandomAccessFile(hello.txt,rw);raf.seek(3);byte[] b new byte[10];int len;StringBuffer sb new StringBuffer();//可变的字符序列相当于Stringwhile((len raf.read(b)) ! -1){sb.append(new String(b,0,len));}raf.seek(3);//把指针调到第三个的位置raf.write(123456789.getBytes());//写入要插入的内容raf.write(sb.toString().getBytes());//写入原来的后面字符}catch(Exception e) {e.printStackTrace();}finally{if(raf ! null){try{raf.close();}catch(Exception e){e.printStackTrace();}}}}Test//实现在指定位置的插入效果(在test2的基础上进行优化使之更通用)public void test3(){RandomAccessFile raf null;try{raf new RandomAccessFile(hello.txt,rw);raf.seek(3);String str raf.readLine();//读取出要插入处之后的所有字符// long l raf.getFilePointer();//获取当前指针的位置// System.out.println(l);//12raf.seek(3);//把指针调到第三个的位置raf.write(123456789.getBytes());//写入要插入的内容raf.write(str.getBytes());//写入原来的后面字符}catch(Exception e) {e.printStackTrace();}finally{if(raf ! null){try{raf.close();}catch(Exception e){e.printStackTrace();}}}} }IO流练习 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class test1 {public static void main(String[] args) { test1 t new test1();System.out.println(请输入一个字符串);String str t.nextString();System.out.println(str);int j t.nextInt();System.out.println(j 20);}public String nextString() {InputStreamReader isr new InputStreamReader(System.in);BufferedReader br new BufferedReader(isr);String str null;try {str br.readLine();}catch(IOException e) {e.printStackTrace();}return str;}public int nextInt() {return Integer.parseInt(nextString());}public boolean nextBoolean() {return Boolean.parseBoolean(nextString());}}感谢大家的支持关注评论点赞 参考资料 尚硅谷宋红康20天搞定Java基础下部
http://www.huolong8.cn/news/166347/

相关文章:

  • 有了源码可以做网站吗速成网站怎么做
  • dede移动端网站源码淄博网站建设有限公司
  • 建设一个门户网站网站开发最新教程
  • 深圳住房和建设局网站 宝安网站建设课程设计实训总结
  • 蒲城网站建设wzjseo网站上报名系统怎么做
  • 邮箱购买网站网络舆情应对及处置方案
  • 企业官网网站模板下载谁能做网站开发
  • 什么网站做的比较好西安市网站搭建
  • 图书馆网站建设调查问卷做网站多少钱一年
  • 宁波市建设局网站网站开发服务器怎么选
  • 医学关键词 是哪个网站做网店运营ppt
  • 销售网站建设常遇到的问题做网站需要的相关知识
  • 在上海做家教的网站广东网页制作二级考试题目
  • 建设网站项目总结北京装修大概多少钱一平方
  • 中职网站建设与维护试卷国家企业信用信息公示系统查询网
  • 网站运营做的是什么工作久久建筑网官网平台
  • 邯郸做网站推广多少钱有经验的做网站
  • 做电商网站搭建晋升服装店网页设计素材
  • 建筑网站网页设计杭州黑马程序员培训机构怎么样
  • 网站建设将来有什么发展wordpress更改固定连接后404
  • windows2008 iis 网站配置网站前后台套装模板
  • 个人网站开发背景及意义旧安卓手机做网站
  • 漯河网站建设漯河外国做网站的平台
  • 太原企业建站程序做宠物商品的网站
  • 51电子网郑州网站优化服务
  • 上海工业网站建设众创空间网站建设
  • 网页设计师主要是做什么长春seo推广
  • 国内有哪些比较好的做定制旅游网站外贸网站建设工作室
  • c网站开发视频网站建设如何定位
  • 哪里网站备案方便快实惠福步外贸论坛