java之IO整理(上)
- /*//创建一个新文件
- public static void main(String[] args) {
- File file=new File("D:\\hello.txt");
- try {
- file.createNewFile();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }*/
- /*//File类的两个常量
- public static void main(String[] args) {
- System.out.println(File.separator);//结果: \
- System.out.println(File.pathSeparator);//结果: :
- }*/
- /*//删除一个文件
- public static void main(String[] args) {
- String fileName="D:"+File.separator+"hello.txt";
- File file=new File(fileName);
- if(file.exists()){
- file.delete();
- }else {
- System.out.println("文件不存在!");
- }
- }*/
- /*//创建一个文件夹
- public static void main(String[] args) {
- String fileName="D:"+File.separator+"hello";
- File file=new File(fileName);
- file.mkdirs();
- }*/
- /*//使用listFiles列出指定目录的全部文件
- //* listFiles输出的是完整路径
- public static void main(String[] args) {
- String fileName="D:"+File.separator;
- File file=new File(fileName);
- File[] str=file.listFiles();
- for(int i=0;i<str.length;i++){
- System.out.println(str[i]);
- }
- }*/
- /*使用isDirectory判断一个指定的路径是否为目录*/
- /*public static void main(String[] args) {
- String fileName="D:"+File.separator;
- File file=new File(fileName);
- if(file.isDirectory())
- System.out.println("yes");
- else {
- System.out.println("no");
- }
- }*/
- /*列出指定目录的全部内容*/
- /*public static void main(String[] args) {
- String fileName="D:"+File.separator;
- File file=new File(fileName);
- print(file);
- }
- public static void print(File f){
- if(f!=null){
- if(f.isDirectory()){
- File[] fileArray=f.listFiles();
- if(fileArray!=null){
- for(int i=0;i<fileArray.length;i++){
- print(fileArray[i]);//递归调用
- }
- }
- }else {
- System.out.println(f);
- }
- }
- }*/
- /*使用RandomAccessFile写入文件
- public static void main(String[] args) throws IOException {
- String fileName="D:"+File.separator+"hello.txt";
- File file=new File(fileName);
- RandomAccessFile randomAccessFile=new RandomAccessFile(file, "rw");
- randomAccessFile.writeBytes("1234567890");
- randomAccessFile.writeInt(42);
- randomAccessFile.writeBoolean(true);
- randomAccessFile.writeChar(88);
- randomAccessFile.writeDouble(12.678);
- randomAccessFile.writeFloat(23.5f);
- randomAccessFile.close();
- }//如果你此时打开hello。txt查看的话,会发现那是乱码。
- */
- /*字节流
- * 向文件中写入字符串
- public static void main(String[] args) throws IOException {
- String fileName="D:"+File.separator+"hello.txt";
- File file=new File(fileName);
- OutputStream outputStream=new FileOutputStream(file);
- String string="Hello";
- byte[] bs=string.getBytes();
- outputStream.write(bs);
- outputStream.close();
- }*/
- /**
- * 字节流
- * 向文件中一个字节一个字节的写入字符串
- * @throws IOException
- * *//*
- public static void main(String[] args) throws IOException {
- String fileName="D:"+File.separator+"hello.txt";
- File file=new File(fileName);
- OutputStream outputStream=new FileOutputStream(file);
- String string="Hello GG";
- byte[] bs=string.getBytes();
- for(int i=0;i<bs.length;i++){
- outputStream.write(bs[i]);
- }
- outputStream.close();
- }*/
- /**
- * 字节流
- * 向文件中追加新内容:
- * @throws IOException
- * */
- /*public static void main(String[] args) throws IOException {
- String fileName="D:"+File.separator+"hello.txt";
- File file=new File(fileName);
- OutputStream outputStream=new FileOutputStream(file,true);
- String string="Hello MM";
- //String str="\r\nRollen"; 可以换行
- byte[] bs=string.getBytes();
- for(int i=0;i<bs.length;i++){
- outputStream.write(bs[i]);
- }
- outputStream.close();
- }*/
- /**
- * 字节流
- * 读文件内容
- * @throws IOException
- * */
- /*public static void main(String[] args) throws IOException {
- String fileName="D:"+File.separator+"hello.txt";
- File file=new File(fileName);
- InputStream inputStream=new FileInputStream(file);
- //byte[] bs=new byte[1024];//预先申请了一个指定大小的空间
- byte[] bs=new byte[(int) file.length()];
- inputStream.read(bs);
- System.out.println("文件长度为:"+file.length());
- inputStream.close();
- System.out.println(new String(bs));
- }*/
- /**
- * 字节流
- *读文件
- * @throws IOException
- * */
- /*public static void main(String[] args) throws IOException {
- String fileName="D:"+File.separator+"hello.txt";
- File file=new File(fileName);
- InputStream inputStream=new FileInputStream(file);
- byte[] bs=new byte[1024];
- int count=0;
- int temp=0;
- while((temp=inputStream.read())!=(-1)){//当读到文件末尾的时候会返回-1.正常情况下是不会返回-1的
- bs[count++]=(byte) temp;
- }
- inputStream.close();
- System.out.println(new String(bs));
- }*/
- /**
- * 字符流
- * 写入数据
- * @throws IOException
- * */
- /*public static void main(String[] args) throws IOException {
- String fileName="D:"+File.separator+"hello.txt";
- File file=new File(fileName);
- Writer out=new FileWriter(file);
- String string="朋友";
- out.write(string);
- out.close();
- }*/
- /**
- * 字符流
- * 从文件中读出内容
- * @throws IOException
- *采用循环读取的方式,因为我们有时候不知道文件到底有多大。
- * */
- /*public static void main(String[] args) throws IOException {
- String fileName="D:"+File.separator+"hello.txt";
- File file=new File(fileName);
- char[] ch=new char[100];
- Reader reader=new FileReader(file);
- int temp=0;
- int count=0;
- while((temp=reader.read())!=(-1)){
- ch[count++]=(char) temp;
- }
- reader.close();
- System.out.println(new String(ch, 0, count));
- }*/
/**
* 将字节输出流转化为字符输出流
* @throws IOException
* */
/*public static void main(String[] args) throws IOException {
String fileName="D:"+File.separator+"hello.txt";
File file=new File(fileName);
Writer out=new OutputStreamWriter(new FileOutputStream(file));
out.write("MM");
out.close();
}*/
/**
* 将字节输入流变为字符输入流
* @throws IOException
* */
/*public static void main(String[] args) throws IOException {
String fileName="D:"+File.separator+"hello.txt";
File file=new File(fileName);
Reader reader=new InputStreamReader(new FileInputStream(file));
char[] cs=new char[100];
int len=reader.read(cs);
System.out.println(new String(cs, 0, len));
reader.close();
}*/
/**
* 使用内容操作流将一个大写字母转化为小写字母
* @throws IOException
* 内容操作流一般用来生成一些临时信息的,这样可以避免删除的麻烦。
* */
/*public static void main(String[] args) throws IOException {
String str="ADFGHJKLUYTG";
ByteArrayInputStream inputStream=new ByteArrayInputStream(str.getBytes());
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
int temp=0;
while((temp=inputStream.read())!=(-1)){
char ch=(char) temp;
outputStream.write(Character.toLowerCase(ch));
}
String outStr= outputStream.toString();
inputStream.close();
outputStream.close();
System.out.println(outStr);
}*/
1、字节流和字符流的区别:
字节流在操作的时候本身是不会用到缓冲区的,是文件本身的直接操作的,但是字符流在操作的 时候,是会用到缓冲区的,是通过缓冲区来操作文件的。
使用字节流好还是字符流好呢?
答案是字节流。首先因为硬盘上的所有文件都是以字节的形式进行传输或者保存的,包括图片等内容。但是字符只是在内存中才会形成的,所以在开发中,字节流使用广泛。
2、文件的复制:
方法一:DOS下就有一个文件复制功能
方法二:用程序
- /**
- * 文件的复制
- * @throws IOException
- * 思路:从一个文件中读取内容,边读边写入另一个文件。
- * */
- public static void main(String[] args) throws IOException {
- String helloFileName="D:"+File.separator+"hello.txt";
- String rollenFileName="D:"+File.separator+"rollen.txt";
- File file1=new File(helloFileName);
- File file2=new File(rollenFileName);
- if(!file1.exists()){
- System.out.println("被复制的文件不存在!");
- System.out.println(1);
- }
- InputStream inputStream=new FileInputStream(file1);
- OutputStream outputStream=new FileOutputStream(file2);
- if(inputStream!=null&&outputStream!=null){
- int temp=0;
- while((temp=inputStream.read())!=(-1)){
- outputStream.write(temp);
- }
- }
- inputStream.close();
- outputStream.close();
- }
3、管道流
管道流主要用以进行两个线程之间的通信。
PipedOutputStream 管道输出流
PipedInputStream 管道输入流
- /*消息发送类*/
- public class Send implements Runnable{
- private PipedOutputStream out=null;
- public Send() {
- out=new PipedOutputStream();
- }
- public PipedOutputStream getOut(){
- return this.out;
- }
- @Override
- public void run() {
- String message="hello MM";
- try {
- out.write(message.getBytes());
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- out.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- /*接受消息类*/
- public class Recive implements Runnable{
- private PipedInputStream input=null;
- public Recive() {
- input=new PipedInputStream();
- }
- public PipedInputStream getInput(){
- return this.input;
- }
- @Override
- public void run() {
- byte[] bs=new byte[1000];
- int len=0;
- try {
- len=this.input.read(bs);
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- input.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("接收的内容是:"+(new String(bs, 0, len)));
- }
- }
- /*测试类*/
- public static void main(String[] args) {
- Send send=new Send();
- Recive recive=new Recive();
- try {
- send.getOut().connect(recive.getInput());//管道连接
- } catch (IOException e) {
- e.printStackTrace();
- }
- new Thread(send).start();
- new Thread(recive).start();
- }
- }
4、打印流
- /* 使用PrintStream进行输出*/
- /*public static void main(String[] args) throws IOException {
- PrintStream printStream=new PrintStream(new FileOutputStream(new File("D:"+File.separator+"hello.txt")));
- printStream.print(true);
- printStream.print("GG");
- printStream.close();
- }*/
- /*使用PrintStream进行输出
- * 并进行格式化*/
- /*public static void main(String[] args) throws IOException {
- PrintStream printStream=new PrintStream(new FileOutputStream(new File("D:"+File.separator+"hello.txt")));
- String name="GG";
- int age=26;
- printStream.printf("姓名:%s;年龄:%d!",name,age);
- printStream.close();
- }*/
- /*使用OutputStream向屏幕上输出内容*/
- public static void main(String[] args) {
- OutputStream out=System.out;
- try {
- out.write("MM".getBytes());
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- out.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
java之IO整理(上)的更多相关文章
- java之IO整理(下)
一:对象的序列化 对象序列化就是把一个对象变为二进制数据流的一种方法. 一个类要想被序列化,就行必须实现java.io.Serializable接口.虽然这个接口中没有任何方法,就如同之前的clone ...
- java之IO整理(中)
一:打印流/*System.out.println()重定向输出*/ /*public static void main(String[] args) { System.out.println(&qu ...
- java中的IO整理
写在前面:本文章基本覆盖了java IO的全部内容,java新IO没有涉及,因为我想和这个分开,以突出那个的重要性,新IO哪一篇文章还没有开始写,估计很快就能和大家见面.照旧,文章依旧以例子为主,因为 ...
- 金九银十,史上最强 Java 面试题整理。
以下会重新整理所有 Java 系列面试题答案.及各大互联网公司的面试经验,会从以下几个方面汇总,本文会长期更新. Java 面试篇 史上最全 Java 面试题,带全部答案 史上最全 69 道 Spri ...
- 【转】 Java中的IO整理
写在前面:本文章基本覆盖了java IO的全部内容,java新IO没有涉及,因为我想和这个分开,以突出那个的重要性,新IO哪一篇文章还没有开始写,估计很快就能和大家见面.照旧,文章依旧以例子为主,因为 ...
- Java基础进阶整理
Java学习笔记整理 本文档是我个人整理的,首先是想通过完成本文档更加扎实自己的基础加强对java语言的理解,然后就是想给入了门的同志们做下贡献. 当然,本文档主要是对java语言基础(当然还有很多基 ...
- 尚学堂Java面试题整理
博客分类: 经典分享 1. super()与this()的差别? - 6 - 2. 作用域public,protected,private,以及不写时的差别? - 6 - 3. 编程输出例如以 ...
- 《OD面试》Java面试题整理
一.面试考察点 1 主语言本身 2 数据库 3 算法 4 Spring/SpringMVC/MyBatis 5 项目经验 1)项目涉及到的技术点深挖: (1)考察候选人技术深度 (2)看候选人遇到问 ...
- java学习内容整理
转自:http://www.cnblogs.com/caoleiCoding/p/6170555.html 首先,我个人比较推崇的学习方法是:先学java前段,也就是HTML,css,js,因为学习j ...
随机推荐
- 【PL/SQL编程】循环语句
1. loop语句 loop plsql_sentence; exit when end_condition_exp; end loop; loop语句会先执行一次循环体,然后再判断“exit whe ...
- Kotlin Reference (一) Basic Syntax
什么是Kotlin Kotlin翻译成中文叫"靠他灵",它是由JetBrains公司发明的一种基于JVM的编程语言,目前Google宣布kotlin为Android开发的官方语言. ...
- Freemaker的java.beans.IntrospectionException: type mismatch between read and write methods
引言:freemaker在特定的spring以及jdk下的问题解决路径. 环境描述 spring 3.1.1, jdk1.8u80, freemake 2.3.19 错误信息描述: 严重: Excep ...
- EasyRMS录播管理服务器项目实战:windows上开机自启动NodeJS服务
本文转自EasyDarwin开源团队成员Penggy的博客:http://www.jianshu.com/p/ef840505ae06 近期在EasyDarwin开源团队开发一款基于EasyDarwi ...
- SoftmaxWithLoss函数和师兄给的loss有哪些区别呢
师兄的: NG教程中提到的:
- JAXP使用Stax API时格式化输出XML 2
之前实现的一个版本:http://www.cnblogs.com/lyhtbc/p/jaxp-pretty-format-validate-validation-stax-stax2.html 这个版 ...
- 通过反编译让SpecFlow支持多层属性值的验证
需求:在使用SpecFlow时,我希望能对目标对象所关联的对象属性进行验证,但SpecFlow(Version 1.9.0)无法实现.如图中红框,可以对专户所属的金融机构的名称进行验证. 反编译步骤 ...
- 拷贝ssh公钥到多台服务器上
这篇文章几乎是对Push SSH public keys to multiple host的翻译,谢谢该作者. 使用SSH登陆.执行命令到远程机器需要输入密码,很多系统需要免输密码访问远程机器,比如h ...
- bzoj 5334 数学计算
bzoj 5334 数学计算 开始想直接模拟过程做,但模数 \(M\) 不一定为质数,若没有逆元就 \(fAKe\) 掉了. 注意到操作 \(2\) 是删除对应的操作 \(1\) ,相当于只有 \(1 ...
- Mac OS安装php-redis扩展
下载php-redis(用于php5.x的版本),地址:https://nodeload.github.com/nicolasff/phpredis/zip/master. 如果是php7.2,选择p ...