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. Flex 布局排版总结

    1.display: flex / inline-flex; flex:  作为弹性盒自适应屏幕 inline-flex:作为弹性盒自适应当前块级元素所包含的子级块 例:flex,子级块宽度自动相加, ...

  2. linux命令学习之:vim

    1. 关于Vim vim是我最喜欢的编辑器,也是linux下第二强大的编辑器. 虽然emacs是公认的世界第一,我认为使用emacs并没有使用vi进行编辑来得高效. 如果是初学vi,运行一下vimtu ...

  3. ubuntu下安装nginx1.11.10

    (本页仅作为个人笔记参考) 为openssl,zlib,pcre配置编译 wget http://om88fxbu9.bkt.clouddn.com/package/nginx/nginx-1.11. ...

  4. 46-wxpython 4 使用 grid 展示表格

    转载:https://blog.csdn.net/soslinken/article/details/79024938#%E4%BD%BF%E7%94%A8%E6%A0%B7%E4%BE%8B wxp ...

  5. springboot 日志2

      SpringBoot关于日志的官方文档 1.简述 SpringBoot官方文档关于日志的整体说明 本博客基于SpringBoot_1.3.6大家请先简单看下这篇英文的官方文档,文中有说 Sprin ...

  6. golang 创建一个简单的连接池,减少频繁的创建与关闭

    一.连接池的描述图片如下: 二.连接池代码如下: package main; import ( "time" "sync" "errors" ...

  7. blockchain good article

    https://medium.com/programmers-blockchain/create-simple-blockchain-java-tutorial-from-scratch-6eeed3 ...

  8. vue2.0插件

    1.better-scroll 参考网址:https://ustbhuangyi.github.io/better-scroll/doc/zh-hans/ better-scroll 是什么 firs ...

  9. vue 获取组件 和 dom 对象 ref/el

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  10. HISAT,sTRINGTIE,ballgown三款RNA-seq信息分析软件

    HISAT,sTRINGTIE,ballgown三款RNA-seq信息分析软件 2015年04月02日 11:35:47 夜丘 阅读数:8940 标签: 生物 更多 个人分类: 论文笔记   Bowt ...