Java实验6 文件

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!");
	}
}














  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Write a class called Person with the following attributes: title (Mr., Mrs., Ms., etc.) first name last name nickname age in years sex (boolean - true/false to indicated either male or female) Write a constructor that takes no parameters and performs no initializations. Write a constructor that takes a parameter for each of the attributes listed above and sets them within the objects by calling the setter methods listed below. The Person class should have a setter method and a getter method with public access for each attribute. In the setter methods, get rid of any leading or trailing spaces (String trim() method). For a Person with the following attributes: title = "Mr." first name = "Michael" last name = "Zheng" nickname = "Mike" age = 22 sex = true (true is male, false is female) The Person class should have the following public access methods that return Strings as follows: standardName() concatenation of the first and last names (i.e., "Michael Zheng") formalName() concatenation of the title, first name, lastname (i.e., "Mr. Michael Zheng") casualName() return the nickname if it is not null, otherwise return the first name (i.e., "Mike") Be realistic when generating names. If a particular attribute does not exist for a given person, don't try to concatenate it. If necessary, add appropriate spacing and punctuation, but do not leave any leading or trailing spaces in the String that is returned. MakePerson Write a class called MakePerson with a main() method that instantiates 2 Person objects. Initialize the attributes of one of the Person objects by supplying parameters to it's constructor. Instantiate the other Person object with the default constructor (that does not accept any parameters), then set it's attributes via the appropriate setter methods. For each of the Person objects, execute and print (System.out.println()) the results of all of the getter methods and of the standardName(), formalName(), and casualName() methods.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值