java编程IO简单回顾和学习

java编程IO操作必不可少的,很久不玩IO,回顾一下,写了几个小程序,记录一下,方便查阅和学习。

1.给出一个整数数组,将其写入一个文件,再从文件中读出,并按整数大小逆序打印。

package com.io.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.apache.jasper.tagplugins.jstl.core.ForEach;

public class SortAndPrint {
    //给出一个整数数组,将其写入一个文件,再从文件中读出,并按整数大小逆序打印。

    public static void main(String[] args) throws IOException {
        int[] arr={3,4,6,78,90,1};

        //新建一个文件
        File file=new File("d:\\sort.txt");
        FileWriter fw=new FileWriter(file);
        String string="";
        for (int i = 0; i < arr.length; i++) {
            int num = arr[i];
            string+=num+",";
        }
        System.out.println(string);

        try {
            fw.write(string);
            fw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                fw.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        //从文件中读取
        FileReader fr=new FileReader(file);
        BufferedReader bfr=new BufferedReader(fr);
        StringBuilder sb=new StringBuilder();

         int ch = 0;
         String str=null;
         while((str=bfr.readLine())!=null )
         {
             sb.append(str);
         }
         fr.close();
         bfr.close();
        String[] split = sb.toString().split(",");
        int[] ints = StringToInt(split);
        //进行排序
        int[] sort2 = sort(ints);
        for (int i = sort2.length-1; i>0; i--) {
            System.out.println(sort2[i]);
        }
    }
    //将字符串数组转换为整数数组
    public static int[] StringToInt(String[] strs){
        int num=strs.length;
        int[] arrs=new int[num];
        for (int i = 0; i < strs.length; i++) {
            arrs[i]=Integer.parseInt(strs[i]);
        }
        return arrs;
    }
    //冒泡排序
    public static int[] sort(int[] arr){
        int num=arr.length;
        int temp=0;
        for (int i = num-1; i>0; i--) {
            for(int j=0;j<i;j++){
                //冒泡法
                if(arr[j]>arr[i]){
                    //互换位置
                    temp=arr[j];
                    arr[j]=arr[i];
                    arr[i]=temp;
                }
            }
        }
        return arr;
    }

}

2.遍历一个 文件夹中的所有文件。

package com.io.test;

import java.io.File;

public class FileBianLi {
    //遍历 文件夹中的所有文件
    public static void main(String[] args) {
        //得到要遍历的目录文件夹
        File file=new File("e:\\");
        showAllFile(file);
    }

    public static void showAllFile(File file){
        //获取所有子文件
        File[] files = file.listFiles();
        //判断文件类型
        if(!file.isDirectory()){
            System.out.println("文件:"+file.getAbsolutePath());
        }else{
            if(files==null||files.length==0){
                System.out.println("空目录:"+file.getAbsolutePath());
            }else{
                //遍历files
                System.out.println("目录:"+file.getAbsolutePath());
                for (File f : files) {
                    showAllFile(f);
                }
            }
        }

    }

}
时间: 2024-10-11 10:58:05

java编程IO简单回顾和学习的相关文章

Java 多线程IO简单实用Demo

多线程主要作用是充分利用Cpu,而不在于它的乱序性.本Demo不讲它竞争什么的.之前看过乱序打印ABC的例子什么的,那些有意义吗? 本Demo 是多线程打印文件夹下的文件,主要实现是用数组存放文件,一个游标遍历. 我们需要考虑在什么时候加互斥访问,本例用synchronized . 先考虑单线程的流程:客户端启动-->读取文件下的文件放到数组(IO)--> 取游标打印 ,游标加1 -- > 改文件写文件(IO) -- 重复上两步至越界 -- 结束 多线程时显然需要在"取游标打印

Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十三)之Strings

Immutable Strings Objects of the String class are immutable. If you examine the JDK documentation for the String class, you’ll see that every method in the class that appears to modify a String actually creates and returns a brand new String object c

Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记四)之Operators

At the lowest level, data in Java is manipulated using operators Using Java Operators An operator takes one or more argument and produces a new value. The arguements are in a different form than ordinary method calls, but the effect is the same. + :

Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(二)之Introduction to Objects

The genesis of the computer revolution was a machine. The genesis of out programming languages thus tends to look like that machine. 计算机革命起源于机器,因此编程语言的产生也始于对机器的模仿 Computers are mind amplification tools and a different kind of expressive medium. 计算机是大

Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十二)之Error Handling with Exceptions

The ideal time to catch an error is at compile time, before you even try to run the program. However, not all errors can be detected at compile time. To create a robust system, each component must be robust. By providing a consistent error-reporting

java的Io流机制的学习

IO流机制 File类的使用 File类的构造方法 File(URI?uri) File(String?pathname) File(File?parent, String?child) File(String?parent, String?child) File类的常用方法 boolean exists():判断文件是否存在 boolean createNewFile() :创建一个新文件,只能创建一个文件,不能创建目录(文件夹),创建时先判断文件 是否存在 ,不存在则创建并返回true, 存

Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十一)之Holding Your Objects

To solve the general programming problem, you need to create any number of objects, anytime, anywhere. So you can't rely on creating a named reference to hold each one of your objects. Java has several ways to hold objects: 1. the compiler-supported

Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(六)之Initialization &amp; Cleanup

Two of these safety issues are initialization and cleanup. initialization -> bug cleanup -> running out of resources (most notably, memory) Java adopted the constructor, and in addition has a garbage collector that automoatically releases memory res

Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(八)之Polymorphism

Polymorphism is the third essential feature of an object-oriented programming language,after data abastraction and inheritance. It provides another dimension of separation of interface from implementation, to decouple what from how. Polymorphism allo