자바 소설 리더

11486 단어 소설 리더
package FictionManagementsSystem;

import java.io.*;
import java.util.*;

import org.apache.log4j.Logger;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class JQNovelTool {
	public static Logger logger = Logger.getLogger(The_test.class);
	private static String savePath = "D:/novels";
	private static String serializePath = "D://novels//serialize.bin";
	private static List novelsList = new ArrayList();

	static {
		File file = new File(savePath);
		if (!file.exists()) {
			file.mkdirs();
		}
		file = new File(serializePath);
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 *     
	 */
	@SuppressWarnings("resource")
	public static boolean upload() throws IOException {
		String novelName;
		String novelAuthor;
		String uploadPath;
		System.out.println("        :");
		novelName = new Scanner(System.in).nextLine();
		logger.debug("         :" + novelName);
		System.out.println("         :");
		novelAuthor = new Scanner(System.in).nextLine();
		logger.debug("          :" + novelAuthor);
		System.out.println("         :");
		uploadPath = new Scanner(System.in).nextLine();
		logger.debug("          :" + uploadPath);
		Novel novel = new Novel(novelsList.size(), novelName, novelAuthor);
		if (uploadFile(novel, uploadPath)) {
			novelsList.add(novel);
			return true;
		}
		return false;
	}
	/**
	 *         
	 */
	public static void showNovels() {
		getAllNovels();
	}
	/**
	 *     
	 */
	public static boolean remove() {
		// 1.        
		getAllNovels();
		// 2.        
		@SuppressWarnings("resource")
		int sel = new Scanner(System.in).nextInt();
		logger.debug("            :" + sel);
		if (sel < 0 || sel >= novelsList.size())
			return false;
		// 3.        
		Novel novel = getNovel(sel);
		// 4.    
		new File(novel.getUploadPath()).delete();
		return novelsList.remove(novel);
	}

	/**
	 *            
	 */
	public static boolean download() throws IOException {
		// 1.        
		getAllNovels();
		// 2.        
		@SuppressWarnings("resource")
		int sel = new Scanner(System.in).nextInt();
		logger.debug("            :" + sel);
		// 3.          
		Novel novel = getNovel(sel);
		if (novel == null)
			return false;
		// 2.           
		System.out.println("       ");
		@SuppressWarnings("resource")
		String path = new Scanner(System.in).nextLine();
		logger.debug("            :" + path);
		return downloadFile(novel, path);
	}
	
	/**
	 *     
	 */
	public static boolean read() throws IOException {
		@SuppressWarnings("resource")
		Scanner sc = new Scanner(System.in);
		// 1.        
		getAllNovels();
		// 2.        
		@SuppressWarnings("resource")
		String input = sc.nextLine();
		logger.debug("        :" + input);
		if (!input.matches("\\d")) {
			return false;
		}
		int sel = Integer.parseInt(input);
		Novel novel = getNovel(sel);
		if (novel == null)
			return false;
		read(novel);
		return true;
	}
	/**
	 *    
	 */
	public static void serialize() throws IOException {
		File file = new File(serializePath);
		FileOutputStream outStream = new FileOutputStream(file);
		ObjectOutputStream objOutStream = new ObjectOutputStream(outStream);
		objOutStream.writeObject(novelsList);
		objOutStream.close();
	}

	/**
	 *      
	 */
	public static void deserialize() throws IOException, ClassNotFoundException {
		/*
		 * File file = new File(serializePath); if (file.length() <= 0) return;
		 * FileInputStream inStream = new FileInputStream(file); try { ObjectInputStream
		 * objInStream = new ObjectInputStream(inStream);
		 * 
		 * @SuppressWarnings("unchecked") List object = (ArrayList)
		 * objInStream.readObject(); novelsList = object; } catch (Exception e) {
		 * e.printStackTrace(); }
		 */
	}

	/**
	 *              
	 */
	private static boolean uploadFile(Novel novel, String oriPath) throws IOException {
		// 1.          
		File oriFile = new File(oriPath);
		if (!oriFile.exists()) {
			return false;
		}
		File tarFile = new File(savePath + File.separator + novel.getName() + ".txt");
		BufferedReader reader = new BufferedReader(new FileReader(oriFile));
		BufferedWriter writer = new BufferedWriter(new FileWriter(tarFile));
		// 3.    
		String line = "";
		while ((line = reader.readLine()) != null) {
			writer.write(line);
		}
		// 4. novel        
		novel.setUploadPath(tarFile.getAbsolutePath());
		// 5.   
		reader.close();
		writer.close();
		return true;
	}

	/**
	 *     
	 */
	@SuppressWarnings("unused")
	private static boolean deleteFile(String path) {
		File file = new File(path);
		if (file.exists()) {
			file.delete();
			return true;
		}
		return false;
	}

	/**
	 *       
	 */
	private static boolean downloadFile(Novel novel, String desPath) throws IOException {
		// 1.               
		File desfile = new File(desPath);
		if (!desfile.exists() || desfile.isFile()) {
			return false;
		}
		// 2.              
		File oriFile = new File(novel.getUploadPath());
		BufferedReader reader = new BufferedReader(new FileReader(oriFile));
		// 3.            
		BufferedWriter writer = new BufferedWriter(new FileWriter(desfile + File.separator + novel.getName() + ".txt"));
		String line = "";
		while ((line = reader.readLine()) != null) {
			writer.write(line);
		}
		// 4.    
		reader.close();
		writer.close();
		return true;
	}
	/**
	 *       list  novel  ,     null
	 */
	private static Novel getNovel(int index) {
		Iterator it = novelsList.iterator();
		while (it.hasNext()) {
			Novel novel = it.next();
			if (novel.getId() == index) {
				return novel;
			}
		}

		return null;
	}
	
	/**
	 *         
	 */
	private static void read(Novel novel) throws IOException {
		int curRow = 0;//          
		int count = getCharCount(novel);//         
		int rowCount = 0;//        
		if (count % 100 == 0) {
			rowCount = count / 100;
		} else {
			rowCount = count / 100 + 1;
		}
		//       ,     100   
		System.out.println(getReadStr(novel, curRow));
		
		//     
		while (true) {
			/*
			 *       
			 */
			ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");
		    Dispatch sapo = sap.getObject();
		    try {
		        //    0-100
		        sap.setProperty("Volume", new Variant(100));
		        //        -10   +10
		        sap.setProperty("Rate", new Variant(2));
		        //     
		        Dispatch.call(sapo, "Speak", new Variant(getReadStr(novel, curRow)));
		    } catch (Exception e) {
		        e.printStackTrace();
		    } finally {
		        sapo.safeRelease();
		        sap.safeRelease();
		    }
			System.out.println("1.     2.      3.      4.    5.    ");
			@SuppressWarnings("resource")
			String input = new Scanner(System.in).nextLine();
			
			if (!input.matches("\\d")) {
				System.out.println("       ");
				continue;
			}
			logger.debug("             :" + input);
			int sel = Integer.parseInt(input);
			
			switch (sel) {
			case 1:
				curRow = 0;
				break;
			case 2:
				curRow -= 1;
				if (curRow < 0) { //       
					curRow = 0;
					System.out.println("    ");
				}
				break;
			case 3:
				curRow += 1;
				if (curRow >= rowCount) { //       
					curRow = rowCount - 1;
					System.out.println("    ");
				}
				break;
			case 4:
				curRow = rowCount - 1;
				break;
			case 5:
				return;
			default:
				System.out.println("      ");
			}
			System.out.println(getReadStr(novel, curRow));
		}
	}

	/**
	 *        ,         (       ,      )
	 */
	private static int getCharCount(Novel novel) throws IOException {
		File oriFile = new File(novel.getUploadPath());
		FileReader fileReader = new FileReader(oriFile);
		int count = 0;
		//                   
		while (fileReader.read() != -1) {
			count++;
		}
		fileReader.close();
		return count;
	}

	/**
	 *            
	 */
	private static String getReadStr(Novel novel, int curRow) throws IOException {
		// 1.           
		File oriFile = new File(novel.getUploadPath());
		FileReader fileReader = new FileReader(oriFile);
		fileReader.skip(curRow * 100); //     ,  skip  
		// 2.    100   
		char buf[] = new char[100];
		fileReader.read(buf);
		fileReader.close();
		// 3.  buf
		return new String(buf);
	}
	/*          100         , read(buf)         100   ,               */

	/**
	 *   list     
	 */
	private static void getAllNovels() {
		System.out.println("    \t    \t    ");
		Iterator it = novelsList.iterator();
		while (it.hasNext()) {
			System.out.println(it.next());
		}
	}
}
package FictionManagementsSystem;

import java.io.*;

public class Novel implements Serializable {
	private static final long serialVersionUID  = 1L;
	private int id;
	private String name;
	private String author;
	private String uploadPath;

	public Novel(int id, String name, String author) {
		this.id = id;
		this.name = name;
		this.author = author;
	}

	public int getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public String getAuthor() {
		return author;
	}

	public String getUploadPath() {
		return uploadPath;
	}

	public void setUploadPath(String uploadPath) {
		this.uploadPath = uploadPath;
	}

	@Override
	public String toString() {
		return this.getId() + "\t" + this.getName() + "\t" + this.getAuthor();
	}
}

package FictionManagementsSystem;

import java.util.Scanner;

import org.apache.log4j.Logger;

public class The_test {
	public static Logger logger = Logger.getLogger(The_test.class);
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		while (true) {
			System.out.println("    Java     ");
			System.out.println("     :");
			System.out.println("1.    \t2.      \t3.    \t4.    \t5.    ");
			// 1.    
			JQNovelTool.deserialize();
			@SuppressWarnings("resource")
			String input = new Scanner(System.in).nextLine();
			if (!input.matches("\\d")) {
				System.out.println("       ");
				continue;
			}
			int sel = Integer.parseInt(input);
			logger.debug("    :" + input);
			switch (sel) {
			case 1: {
				if (JQNovelTool.upload()) {
					System.out.println("    ");
					//    
					JQNovelTool.serialize();
				} else
					System.out.println("    ");
			}
				break;
			case 2: {
				System.out.println("      ");
				JQNovelTool.showNovels();
			}
				break;
			case 3: {
				System.out.println("            ");
				if (JQNovelTool.remove()) {
					System.out.println("    ");
					//    
					JQNovelTool.serialize();
				} else {
					System.out.println("        ");
				}
			}
				break;
			case 4: {
				System.out.println("            ");
				if (JQNovelTool.download())
					System.out.println("    !");
				else
					System.out.println("           ");
			}
				break;
			case 5: {
				System.out.println("            ");
				if (!JQNovelTool.read())
					System.out.println("        ");
			}
				break;
			default:
				System.out.println("         ,           ,    !");
				break;
			}
		}
	}
}

좋은 웹페이지 즐겨찾기