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);
}
}
} } }

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

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

    Immutable Strings Objects of the String class are immutable. If you examine the JDK documentation fo ...

  2. java小知识点简单回顾

    1.java的数据类型分为两种:简单类型和引用类型(数组.类以及接口).注意,java没有指针的说法,只有引用.简单类型的变量被声明时,存储空间也同时被分配:而引用类型声明变量(对象)时,仅仅为其分配 ...

  3. java的Io流机制的学习

    IO流机制 File类的使用 File类的构造方法 File(URI uri) File(String pathname) File(File parent, String child) File(S ...

  4. 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 ...

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

    At the lowest level, data in Java is manipulated using operators Using Java Operators An operator ta ...

  6. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(三)之Everything Is an Object

    ---恢复内容开始--- Both C++ and Java are hybird languages. A hybird language allow multiple programming st ...

  7. 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 ...

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

    Learn Java I found out that I and other speakers tended to give the typical audience too many topics ...

  9. java 的 IO简单理解

    首先要先理解什么是 stream ? stream代表的是任何有能力产出数据的数据源,或是任何有能力接收数据的接收源. 一.不同导向的 stream 1)以字节为单位从 stream 中读取或往 st ...

随机推荐

  1. [leetcode]127. Word Ladder单词接龙

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  2. InertialNav

    https://github.com/priseborough/InertialNav Instructions To Run Simulink Model Note : Simulink model ...

  3. css 边距等常用设置

    前端知识 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...

  4. slam14讲证明构成李代数

  5. 解决loadrunner录制页面的乱码问题

    以下亲自验证了的:好用.     三步解决loadrunner录制页面的乱码问题 第一步:去lr 的vugen的Tools -> Recoding Options -> Advanced ...

  6. PAT 1071 小赌怡情(15)(代码)

    1071 小赌怡情(15 分) 常言道"小赌怡情".这是一个很简单的小游戏:首先由计算机给出第一个整数:然后玩家下注赌第二个整数将会比第一个数大还是小:玩家下注 t 个筹码后,计算 ...

  7. centos vncviewer

    CentOS6.5 安装vncserver实现图形化访问   一. 安装gnome图形化桌面 #yum groupinstall -y "X Window System" #yum ...

  8. 用户态tcp协议栈调研

    一.各种用户态socket的对比 1.MTCP 简单介绍: 韩国高校的一个科研项目,在DPDK的2016年的技术开发者大会上有讲,所以intel将这个也放到了官方上,所以一般搜索DPDK的用户态的协议 ...

  9. 洛谷2971 [USACO10HOL]牛的政治Cow Politics

    原题链接 假设只有一个政党,那么这题就退化成求树的直径的问题了,所以我们可以从此联想至\(k\)个政党的情况. 先处理出每个政党的最大深度,然后枚举每个政党的其它点,通过\(LCA\)计算长度取\(\ ...

  10. ubuntu 启动时提示 Failed to load session ubuntu

    启动时候提示 Failed to load session ubuntu 通过 CTRL + ALT + [F1~F6] 进入终端界面登陆进系统, 进去之后执行 sudo apt-get instal ...