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);
}
}
}
}
}
java编程IO简单回顾和学习的更多相关文章
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十三)之Strings
Immutable Strings Objects of the String class are immutable. If you examine the JDK documentation fo ...
- java小知识点简单回顾
1.java的数据类型分为两种:简单类型和引用类型(数组.类以及接口).注意,java没有指针的说法,只有引用.简单类型的变量被声明时,存储空间也同时被分配:而引用类型声明变量(对象)时,仅仅为其分配 ...
- java的Io流机制的学习
IO流机制 File类的使用 File类的构造方法 File(URI uri) File(String pathname) File(File parent, String child) File(S ...
- 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 ...
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(四)之Operators
At the lowest level, data in Java is manipulated using operators Using Java Operators An operator ta ...
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(三)之Everything Is an Object
---恢复内容开始--- Both C++ and Java are hybird languages. A hybird language allow multiple programming st ...
- 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 ...
- 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 ...
- java 的 IO简单理解
首先要先理解什么是 stream ? stream代表的是任何有能力产出数据的数据源,或是任何有能力接收数据的接收源. 一.不同导向的 stream 1)以字节为单位从 stream 中读取或往 st ...
随机推荐
- iOS 组件化流程详解(git创建流程)
[链接]组件化流程详解(一)https://www.jianshu.com/p/2deca619ff7e
- 17-matlab例题练习
练习 %编写程序使任意输入的一个数反转,如输入123456,输出654321 clc,clear; a = input('输入一个整数\n'); b = 0; while a ~= 0 b = b ...
- powerdesigner中实现PDM到MYSQl数据库的转换
一.使用PowerDesigner制作建库脚本 1.设计CDM(Conceptual Data Model) 2.选择 Tools -> Generate Physical Data Model ...
- MVc Identity登陆锁定
2016-08-03 [ASP.NET Identity] OAuth Server 鎖定(Lockout)登入失敗次數太多的帳號 743 6 ASP.NET Identity 檢舉文章 2016-0 ...
- vue2.0细节剖析
1.样式切换 单个切换样式 /*html部分*/ <div class="bg" v-bind:class="{active:isActive}"> ...
- python 网络下载的三种风格 未完成
import osimport timeimport sys import requests#依序下载POP20_CC = ('CN IN US ID BR PK NG BD RU JP' 'MX P ...
- 制作根文件系统之内核如何启动init进程
start_kernel其实也是内核的一个进程,它占用了进程号0,start_kernel的内容简写如下: asmlinkage void __init start_kernel(void) //内核 ...
- java的几个日志框架log4j、logback、common-logging
开发工作中每个系统都需要记录日志,常见的日志工具有log4j(用的最多),slf4j,commons-loging,以及最近比较流行的logback 以前只是在项目中用log4j,更多的是参考下配置文 ...
- Linux_(3)Shell编程(上)
一.shell 简介Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁.Shell 既是一种命令语言,又是一种程序设计语言.Shell 是指一种应用程序,这个应用程序提供了一个 ...
- DOM-动态操作心得
这个知识点都是之前看过的,就当是复习了 一.创建元素的三种方法 第一种: document.write() 识别标签 但会覆盖之前内容 第二种: 用元素自身的innerHTML方法 不识别标签 但 ...