package 练习;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
public class 一天 {
public static void main(String[] args) throws IOException {
//g("f:\\");
g(new ArrayList());
g(new LinkedList());
g(new HashSet());
g(new Sd());
g(new HashMap());
g(new File("f:\\"));
g("f:\\" , "1.txt");
gf("f:\\3.txt","f:\\1.txt");
gf();
gd("f:\\1.txt");
gd();
}
private static void gd() {
// TODO Auto-generated method stub
}
private static void gd(String string) throws IOException {
// TODO Auto-generated method stub
File file = new File(string);
FileReader fr = new FileReader(file);
BufferedReader br= null;
br = new BufferedReader(fr);
char[] byt = new char[1024];
while((br.read(byt))!=-1)
{
System.out.println(new String(byt));
}
}
private static void gf() throws IOException {
// TODO Auto-generated method stub
BufferedWriter bw = null;
try{
bw = new BufferedWriter(new FileWriter("f:\\1.txt",true));
while(true)
{
Scanner sc = new Scanner(System.in);
System.out.println("前输入数据:");
String str = sc.nextLine();
if("退出".equals(str))break;
char[] byt = new char[1024];
byt = str.toCharArray();
int len = byt.length;
bw.write(byt,0,len);
bw.flush();
}
}
finally{if(bw!=null){try{bw.close();}catch(IOException ioe){
ioe.printStackTrace();
}}}
}
private static void gf(String str1,String str2) throws IOException {
// TODO Auto-generated method stub
File file1 = new File(str1);
File file2 = new File(str2);
if(!file1.exists())
{
file1.getParentFile().mkdirs();
file1.createNewFile();
}
if(!file2.exists())
{
file2.getParentFile().mkdirs();
file2.createNewFile();
}
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try
{
bis = new BufferedInputStream(new FileInputStream(str1));
bos = new BufferedOutputStream(new FileOutputStream(str2));
byte[] byt = new byte[1024];
int len = byt.length;
while((len = bis.read(byt))!=-1)
{
bos.write(byt,0,len);
bos.flush();
}
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
finally
{
if(bis!=null)
{try{bis.close();}catch(IOException ioe){ioe.printStackTrace();}}
if(bos!=null){try{bos.close();}catch(IOException ioe){ioe.printStackTrace();}}
}
}
private static void g(String str1, String str2) {
// TODO Auto-generated method stub
File file = new File(str1 ,str2);
if(file.exists())
{
file.delete();
}
else
{
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("创建" + file.getName() + "失败");
}
}
}
private static void g(File file) {
// TODO Auto-generated method stub
File[] file1 = file.listFiles();
if(file1!= null)
{
for(File file2 : file1)
{
if(file2.isDirectory())
{
File[] file3 = file2.listFiles(new FilenameFilter(){
@Override
public boolean accept(File f, String st) {
// TODO Auto-generated method stub
boolean b1 =new File(f,st).isFile();
boolean b2 = st.endsWith(".java");
boolean b3 = b1&& b2;
return b3;
}
});
if(file3!=null)
{
for(File file4 : file3)
{
System.out.println(file4.getName() + ":"+file4.getAbsolutePath());
}
}
g(file2);
}
}
}
}
private static void g(HashMap hashMap) {
// TODO Auto-generated method stub
HashMap<Character , Sd> hxm = new HashMap<>();
hxm.put('c', new Sd("nsd",13));
hxm.put('a', new Sd("dfa",55));
Set<Character> set =hxm.keySet();
Iterator<Character> it = set.iterator();
while(it.hasNext())
{
Character a = it.next();
System.out.println(hxm.get(a).getName()+hxm.get(a).getAge());
}
}
private static void g(Sd student) {
// TODO Auto-generated method stub
TreeSet<Sd> ts =new TreeSet<>(new Comparator<Sd>()
{
public int compare(Sd s1, Sd s2)
{
return s1.getName().compareTo(s2.getName());
}
});
//ts.add(new Sd("a",55));
ts.add(new Sd("sd",22));
ts.add(new Sd("a",55));
Iterator<Sd> it =ts.iterator();
while(it.hasNext())
{
Sd s =it.next();
System.out.println(s.getName() + s.getAge());
}
for(Sd ss:ts)
{
System.out.println(ss.getName() + ss.getAge());
}
}
private static void g(HashSet hashSet) {
// TODO Auto-generated method stub
HashSet<String> hs = new HashSet<>();
hs.add("sdsad");
hs.add("A");
hs.add("A");
Iterator<String> it = hs.iterator();
while(it.hasNext())
{
String str = it.next();
System.out.println(str);
}
}
private static void g(LinkedList linkedList) {
LinkedList list = new LinkedList();
list.add("sdasd");
list.add("df");
Iterator<String> it = list.iterator();
while(it.hasNext())
{
String str = it.next();
System.out.println(str);
}
}
public static void g(ArrayList l)
{
ArrayList<String> list = new ArrayList<>();
list.add("神雕侠侣");
list.add("天龙八部");
Iterator<String> it = list.iterator();
while(it.hasNext())
{
String str = it.next();
System.out.println(str);
}
for(int i=0;i<list.size();i++)
{
String str = (String)list.get(i);
System.out.println(str);
}
}
private static void g(String str) {
File file1 = new File(str);
File[] file2 = file1.listFiles();
if(file2!=null)
{
for(File file3 : file2)
{
if(file3.isDirectory())
{
g(file3.getAbsolutePath());
}
else
{
if(file3.getName().endsWith(".mp3"))
{
System.out.println(file3.getName());
}
}
}
}
}
}
|
|