标签:String void System IO new public out
文件:就是保存数据的地方
创建文件:
public class FileCreate { public static void main(String[] args) { } @Test public void create01(){ String filePath = "d:\\news1.txt"; File file = new File(filePath); try { file.createNewFile(); System.out.println("文件创建成功"); } catch (IOException e) { e.printStackTrace(); System.out.println("文件创建失败"); } } @Test //方式2:new File(File parent, String child)//根据父目录文件+子路径构建 //只有执行了createNewFile方法,才能真正的在磁盘创建该文件对象 public void create02(){ File parentFile = new File("d:\\"); String fileName = "new2.txt"; File file = new File(parentFile, fileName); try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } @Test //方式三:new File(String parent,String child)//根据父目录+子路径构建 public void create03(){ String parenPath = "d:\\"; String fileName = "new3.txt"; File file = new File(parenPath, fileName); try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } }
文件的一些方法:
public class FileInformation { public static void main(String[] args) { } @Test //获取文件的信息 public void info(){ //先创建文件对象 File file = new File("d:\\news1.txt"); //调用相应的方法,得到对应的信息 System.out.println("文件名字="+file.getName()); System.out.println("绝对路径="+file.getAbsolutePath()); System.out.println("文件父级目录="+file.getParent()); System.out.println("文件的大小="+file.length()); System.out.println("文件是否存在="+file.exists()); System.out.println("是不是一个文件="+file.isFile()); System.out.println("是不是一个目录="+file.isDirectory()); } }
目录操作:
public class FileInformation { public static void main(String[] args) { } @Test //获取文件的信息 public void info(){ //先创建文件对象 File file = new File("d:\\news1.txt"); //调用相应的方法,得到对应的信息 System.out.println("文件名字="+file.getName()); System.out.println("绝对路径="+file.getAbsolutePath()); System.out.println("文件父级目录="+file.getParent()); System.out.println("文件的大小="+file.length()); System.out.println("文件是否存在="+file.exists()); System.out.println("是不是一个文件="+file.isFile()); System.out.println("是不是一个目录="+file.isDirectory()); } }
IO流原理及流的分类
按操作数据单位不同分为:字节流(8bit)二进制文件,字符流(按字符)文本文件
常见IO流体常见类
InputStream:常用子类
FileInputStream:文件输入流
BufferedlnputStream:缓冲字节输入流
ObjectInputStream:对象字节输入流
FileInputStream:
public class FileInformation { public static void main(String[] args) { } @Test public void FileInputStream(){ String filePath="d:\\news1.txt"; FileInputStream fileInputStream = null; int readlen = 0; try { //创建fileInputStream对象,用于读取文件 fileInputStream = new FileInputStream(filePath); while ((readlen = fileInputStream.read())!=-1){ System.out.print((char)readlen);//转成char显示 } } catch (IOException e) { e.printStackTrace(); } finally { //关闭文件流,释放资源 try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } @Test public void FileInputStream2(){ String filePath="d:\\news1.txt"; FileInputStream fileInputStream = null; int readlen = 0; byte[] buf = new byte[8]; //一次读取八给字节 try { //创建fileInputStream对象,用于读取文件 fileInputStream = new FileInputStream(filePath); while ((readlen = fileInputStream.read(buf))!=-1){ System.out.print(new String(buf,0,readlen));//转成char显示 } } catch (IOException e) { e.printStackTrace(); } finally { //关闭文件流,释放资源 try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
public class FileInformation { public static void main(String[] args) { } @Test public void FileInputStream() { //创建FileOutputStream String filePath = "d:\\a.txt"; FileOutputStream fileOutputStream = null; try { //FileOutputStream(filePath,true)输入内容不覆盖,追加 fileOutputStream = new FileOutputStream(filePath,true); //一次当个字节 fileOutputStream.write('H'); //一次多个字节 String str = "mqsoogwoehowi"; fileOutputStream.write(str.getBytes()); } catch (IOException e) { e.printStackTrace(); } finally { } } }
完成文件拷贝:
public class FileInformation { public static void main(String[] args) { } @Test public void FileCopy() { //完成文件拷贝 /* 创建文件的输入流,将文件读入到程序 创建文件的输出流,将读取到的文件数据,写入到指定的文件 */ FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; int read = 0; String src= "d:\\demo"; String srcFilePath = "d:\\1.png"; String destFilePath = "d:\\demo2.png"; try { fileInputStream = new FileInputStream(srcFilePath); fileOutputStream = new FileOutputStream(destFilePath); File file = new File(src); while (file.mkdir()){ System.out.println("文件夹创建成功"); } byte[] buf = new byte[1024]; while ((read = fileInputStream.read(buf))!=-1){ //读取到后开始向目标文件里面写 fileOutputStream.write(buf,0,read); } } catch (IOException e) { e.printStackTrace(); } finally { try { fileInputStream.close(); fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
FileReader和FileWriter是字节流,即按照字符来操作io
public class FileInformation { public static void main(String[] args) { } @Test public void fileReader1() { //FileReader用法 String filePath = "d:\\news1.txt"; FileReader fileReader = null; int data = 0; //创建FileReader对象 try { fileReader = new FileReader(filePath); while ((data=fileReader.read())!=-1){ //循环读取单个字符 System.out.print((char) data); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { fileReader.close(); } catch (IOException e) { e.printStackTrace(); } } } @Test public void fileReader2(){ //FileReader用法 String filePath = "d:\\news1.txt"; FileReader fileReader = null; int readLen = 0; char[] buf = new char[8]; //创建FileReader对象 try { fileReader = new FileReader(filePath); while ((readLen=fileReader.read(buf))!=-1){ //循环读取单个字符 System.out.print(new String(buf,0,readLen)); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { fileReader.close(); } catch (IOException e) { e.printStackTrace(); } } } }
public class FileInformation { public static void main(String[] args) { } @Test public void fileWriter() { String filePath = "d:\\note.txt"; //创建fileWriteer对象 FileWriter fileWriter = null; try { fileWriter = new FileWriter(filePath); //写入单个字符 fileWriter.write('H'); //写入指定数组 char[] buf = {'a','b','c'}; fileWriter.write(buf); //写入指定数组的指定部分 fileWriter.write("马青松".toCharArray(),0,3); //写入单个字符串 fileWriter.write("目前是"); //写入字符串指定部分 fileWriter.write("后i我hi哦佛i我",0,3); } catch (Exception e) { e.printStackTrace(); } finally { try { fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } }
public class FileInformation { public static void main(String[] args) { } @Test public void fileWriter() throws Exception{ String filePath = "d://news1.txt"; //创建BufferedReader BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath)); //按行读取 String line; while ((line=bufferedReader.readLine())!=null){ System.out.println(line); } bufferedReader.close(); } }
BufferedWiter用法类似
Buffered~~~拷贝
public class FileInformation { public static void main(String[] args) { } @Test public void fileWriter() throws Exception{ String filePath = "d://news1.txt"; String destPath = "d://a.txt"; //创建BufferedReader BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(destPath)); //按行读取 String line; while ((line=bufferedReader.readLine())!=null){ //System.out.println(line); bufferedWriter.write(line); //插入换行 bufferedWriter.newLine(); } if (bufferedReader!=null){ bufferedReader.close(); } if (bufferedWriter!=null){ bufferedWriter.close(); } } }
public class FileInformation { public static void main(String[] args) { } @Test public void fileWriter() throws Exception{ String destPath = "d:\\kk.png"; String scrPath = "d:\\2.png"; //创建对象 BufferedInputStream bufferedInputStream = null; BufferedOutputStream bufferedOutputStream = null; bufferedInputStream = new BufferedInputStream(new FileInputStream(scrPath)); bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(destPath)); int reade = 0; byte[] buff = new byte[1024]; while ((reade = bufferedInputStream.read(buff))!=-1){ bufferedOutputStream.write(buff,0,reade); } bufferedInputStream.close(); bufferedOutputStream.close(); } }
对象处理流
at"; ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(filePath)); //序列化数据 objectOutputStream.writeInt(100); objectOutputStream.writeBoolean(true); objectOutputStream.writeChar('a'); objectOutputStream.writeDouble(9.5); objectOutputStream.writeUTF("目前是"); //保存一个dog对象 objectOutputStream.writeObject(new Dog("mqd",20)); objectOutputStream.close(); } //反序列化 @Test public void fileWriter2() throws Exception{ String filePath = "d://data.dat"; ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(filePath)); System.out.println(objectInputStream.readInt()); System.out.println(objectInputStream.readBoolean()); System.out.println(objectInputStream.readChar()); System.out.println(objectInputStream.readDouble()); System.out.println(objectInputStream.readUTF()); Object o = objectInputStream.readObject(); System.out.println(o); objectInputStream.close(); } } class Dog implements Serializable{ private String name; private int age; public Dog(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Dog{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
标准输入输出
乱码转换
public class FileInformation { public static void main(String[] args) { } @Test public void fileWriter() throws Exception { String filePath = "d:\\a.txt"; //1.把FileInputStream转成InputStreamReader //2.指定编码gbk InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(filePath),"UTF-8"); //3.把InputStreamReader转入BufferedReader BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String s; while ((s = bufferedReader.readLine())!=null){ System.out.println(s); } } }
字节打印流
public class FileInformation { public static void main(String[] args) { } @Test public void fileWriter() throws Exception { PrintStream out = System.out; //在默认情况下,PrintStream输出数据的位置是 标准输出 即显示器 out.print("mqsopop"); out.write("dskljs".getBytes()); //修改打印位置 System.setOut(new PrintStream("d:\\a.txt")); System.out.println("mqsivjowiehvo"); out.close(); } }
字符打印流
public class FileInformation { public static void main(String[] args) { } @Test public void fileWriter() throws Exception { //PrintWriter printWriter = new PrintWriter(System.out); PrintWriter printWriter = new PrintWriter(new FileWriter("d:\\aa.txt")); printWriter.print("cdsvhosvgyvg"); printWriter.close(); } }
标签:String,void,System,IO,new,public,out 来源: https://www.cnblogs.com/maqingsong/p/16531749.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。