Java关于文件....
printfile算法有问题....而且肯定不会这么麻烦.....嘛嘛,先这样
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
public class TestSix {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException
{
printFiles();
dictionary();
sort();
caculate();
}
public static void printFiles()
{
String s;
Vector<String> myVector = new Vector<String>();
Vector<Integer> numVector = new Vector<Integer>();
File myFile = null;
File currentFile = null;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("文件路径:");
try
{
s = in.readLine();
myFile = new File(s);
currentFile = myFile;
}
catch(IOException e)
{
//System.out.print("输入出现异常!");
e.printStackTrace();
}
int tabNumber = 0;
int currentFileNumber = 0;
boolean isok = false;
begin:
while(!isok)
{
//保存当前文件/目录
StringBuffer currentFileName = new StringBuffer();
for(int i = 0; i<tabNumber; i++)
{
currentFileName.append('\t');
}
myVector.add(currentFileName+currentFile.getName());
File[] files = currentFile.listFiles();
//如果当前的是一个文件夹,并且不为空,就继续深入下去...
if(currentFile.isDirectory() && files.length!=0)
{
currentFileNumber = 0;
numVector.add(currentFileNumber);
currentFile = files[currentFileNumber];
tabNumber++;
}
else
{
//转到下一个文件
File[] file = currentFile.getParentFile().listFiles();
currentFileNumber = numVector.lastElement();
//返回上一级菜单
while(currentFileNumber==file.length-1)
{
tabNumber--;
File father = currentFile.getParentFile();
father = father.getParentFile();
file = father.listFiles();
numVector.remove(numVector.size()-1);
if(numVector.isEmpty())
{
isok = true;
break begin;
}
currentFileNumber = numVector.lastElement();
}
currentFile = file[numVector.lastElement()+1];
currentFileNumber = numVector.lastElement();
numVector.remove(numVector.size()-1);
numVector.add(currentFileNumber+1);
}
}
//输出结果:
for(int i = 0; i<myVector.size(); i++)
{
System.out.println(myVector.elementAt(i));
}
}
public static void sort()
{
//100个0到200的数字排序
List<Integer> list = new ArrayList<Integer>();
int random = (int)(201*Math.random());
list.add(0, random);
for(int i = 0; i<100; i++)
{
random = (int)(201*Math.random());
int j = 0;
for(j = i; j>=0; j--)
{
if(random>list.get(j))
{
list.add(j+1, random);
break;
}
}
if(j == -1 )
{
list.add(0,random);
}
}
//遍历
for(int i = 0; i<100; i++)
{
System.out.print(list.get(i)+"\t");
if(i%5 == 4)
{
System.out.println();
}
}
//100个六个单词的英文排序
List<String> wordList = new ArrayList<String>();
StringBuffer word = new StringBuffer();
for(int i = 0; i<6; i++)
{
random = (int)(26*Math.random());
word.append((char)(65+random));
}
wordList.add(0, word.toString());
for(int i = 0; i<100; i++)
{
word = new StringBuffer();
for(int c = 0; c<6; c++)
{
random = (int)(26*Math.random());
word.append((char)(65+random));
}
int j = 0;
for(j = i; j>=0; j--)
{
if(word.toString().compareTo(wordList.get(j).toString()) > 0)
{
wordList.add(j+1, word.toString());
break;
}
}
if(j == -1 )
{
wordList.add(0,word.toString());
}
}
//遍历
for(int i = 0; i<100; i++)
{
System.out.print(wordList.get(i)+"\t");
if(i%5 == 4)
{
System.out.println();
}
}
}
public static void caculate() throws IOException
{
FileReader inFile = new FileReader("SomeData.txt");
File outFile = new File("AvgSomeData.txt");
BufferedReader in = new BufferedReader(inFile);
FileWriter out = new FileWriter(outFile);
String line;
int lineNumber = 1;
while((line = in.readLine()) != null)
{
String[] vStrs = line.split("\t");
float result = Float.parseFloat(vStrs[3]) + Float.parseFloat(vStrs[4]) +Float.parseFloat(vStrs[5]) + Float.parseFloat(vStrs[6]);
out.write(String.format("%3s\t", lineNumber));
out.write(String.format("%1$4.2f\r\n",result/4));
lineNumber++;
}
in.close();
out.close();
}
public static void dictionary()
{
String[] dictionary = {"cat","猫","dog","狗","horse","马","lion","狮子","panda","熊猫",
"bear","熊","rat","老鼠","tiger","老虎","deer","鹿","bee","蜜蜂",
"crow","乌鸦","frog","青蛙","monkey","猴子","chicken","小鸡","giraffe","长颈鹿",
"cricket","蟋蟀","butterfly","蝴蝶","fox","狐狸","hippo","河马","owl","猫头鹰",
"rabbit","兔子","swallow","燕子","whale","鲸","swan","天鹅","crane","鹤",
"squirrel","松鼠","goat","山羊","pigeon","鸽子","crab","螃蟹","inkfish","乌贼"};
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("要查询的单词:");
String word = "";
try
{
word = in.readLine();
}
catch(IOException e)
{
//System.out.print("输入出现异常!");
e.printStackTrace();
}
for(int i = 0; i<30; i+=2)
{
if(dictionary[i].compareTo(word) == 0)
{
System.out.println("词义是:"+dictionary[i+1]);
return ;
}
}
System.out.println("not found in our dictionary!");
}
}