例如:
a.txt文件:
3241256364789629090126581212515
奇数:xx个
偶数:xx个
package com.cskaoyan._17day;
import java.io.*;
import java.util.ArrayList;
public class Exercise {
public static void main(String[] args) throws IOException {
try {
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(new File("a.txt"))));
String lineTxt="3241256364789629090126581212515";
int a = 0;
ArrayList<String> oddnumber=new ArrayList<>();
ArrayList<Object> evennumber=new ArrayList<>();
while ((lineTxt=br.readLine())!=null){
a++;
if (a % 2 == 0) {
oddnumber.add(lineTxt);
} else {
evennumber.add(lineTxt);
}
}
br.close();
}catch (Exception e){
System.out.println("read error: " + e);
}
}
}
package com.cskaoyan._17day;
import java.io.*;
public class Exercise02 {
public static void main(String[] args) throws IOException {
FileInputStream sc = new FileInputStream("a.txt");
Reader reader = new InputStreamReader( sc );
FileOutputStream s = new FileOutputStream("a.txt", true);
Writer writer=new OutputStreamWriter( s );
char[] chars = new char[1024];
int len;
while((len = reader.read(chars)) != -1){
bubbleSort(chars,len);
writer.write("\n");
writer.write(chars,0,len);
}
reader.close();
writer.close();
}
private static void bubbleSort(char arr[] , int len) {
for (int i=0; i < len -1; i++) {
for (int j=0; j < len -1; j++) {
if (arr[j] > arr[j + 1]){
char temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
}
package com.cskaoyan._17day;
import java.io.*;
import java.util.ArrayList;
public class Exercise03 {
public static void main(String[] args) {
File sc = new File("D:\\");
File file = new File("D:\\ ");
file.mkdir();
String targetDir = "";
eachFile(sc);
//复制所有。java文件到目标文件夹
for (int i=0; i < file.length(); i++) {
FileInputStream input = null;
FileOutputStream output = null;
try{
input = new FileInputStream(file.get(i));
InputStream inbuffer = new BufferedInputStream(input);
//目标文件由输出流自己创建
output = new FileInputStream(targetDir + file.get(i).getName());
OutputStream outbuffer=new BufferedOutputStream(output);
//利用字节缓冲流复制文件
byte[] b = new byte[1024];
int len;
while ((len = inbuffer.read(b)) != -1){
outbuffer.write(b,0,len);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
closeQuietly(input);
closeQuietly(output);
}
}
}
public static void closeQuietly(Closeable closeable){
try{
if (closeable != null){
closeable.close();
}
} catch (IOException e){
e.printStackTrace();
}
}
public static ArrayList<File> files = new ArrayList<>();
//遍历这个文件夹内的所有子目录和文件
public static void eachFile(File file){
try{
File[] targetFile = file.listFiles();
for (int i=0; i < targetFile.length; i++) {
if (targetFile[i].isDirectory()){
eachFile(targetFile[i]);
}else{
//找到所有的java文件并且放到集合中
if (targetFile[i].getName().endsWith("java")){
file.add(targetFile[i]);
}
}
}
} catch (Exception e){
e.printStackTrace();
}
}
}