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. jmeter简单压测设置

    参数化 随机参数 时间参数 顺序自增函数  文件读取  直接引用 响应断言 用来查看sessionid 关联 关联引用 jmeter操作数据库 安装连接程序包 ip 端口号 哪个数据库 可以执行多条s ...

  2. 使用IntelliJ IDEA 搭建 spring mvc开发环境

    填好GroupId.ArtifactId,一路Next创建工程,在main 目录创建 java文件夹并转换为Sources Root,创建好工程目录结构如下: 配置工程 配置 pom.xml,引入相关 ...

  3. solr简介、学习详细过程!(超详细~)

    solr是什么呢? 一.Solr它是一种开放源码的.基于 Lucene Java 的搜索服务器,易于加入到 Web 应用程序中. 二.Solr 提供了层面搜索(就是统计).命中醒目显示并且支持多种输出 ...

  4. Linux下新建服务

    1 首先在/etc/rc.d/init.d/下添加脚本 asr_cron #!/bin/bash # $Id: rc.redhat.asterisk -- ::43Z tilghman $ # # a ...

  5. C# 一个数组未赋值引发的错误

    在电脑前又是一天,后来脑子也糊里糊涂,可能是基础还不牢固,设置断点,找了找问题才发现数组定义出了问题, 我是这样定义数组的,string[] auths ; string auths=new stri ...

  6. Oracle_高级功能(2) 索引

    1.oracle优化器 优化目标分为4种: choose (选择性) rule (基于规则) first rows(第一行) all rows(所有行) Description:描述sql的执行计划 ...

  7. 程序员"装B"手册

    一.准备工作 “工欲善其事必先利其器.” 1.电脑不一定要配置高,但是双屏是必须的,越大越好,能一个横屏一个竖屏更好.一个用来查资料,一个用来写代码.总之要显得信息量很大,效率很高. 2.椅子不一定要 ...

  8. TCHAR函数查询

    https://blog.csdn.net/is2120/article/details/27542927

  9. Jquery中$.each()与$().each()的使用与区别。

    在jquery中,遍历对象和数组,经常会用到$().each和$.each(),两个方法.两个方法是有区别的,从而这两个方法在针对不同的操作上,显示了各自的特点. $().each,对于这个方法,在d ...

  10. 如何将service绑入到spring 并且在action中使用

    第一步:定制 service接口,为什么用接口我也不清楚 package com.inspur.services; import com.hsp.domain.User; public interfa ...