能不能给点对于初学者研究的好程序 所有经典的 你觉得不错的 谢了
能不能给点对于初学者研究的好程序 所有经典的 你觉得不错的 谢了 一直没好的程序看下
[解决办法]
jdk下 demo、sample两个目录的代码都可以看看。
[解决办法]
论坛里不是有个俄罗斯方块么 = =
[解决办法]
初學者的話對大型源碼未必能很好吸收,可以考慮看點好書,像effective java之類
[解决办法]
下面几个是我平时用来读写文件的
///////////////////////////////////////////////////////////////////////////////////////////////////// public static String getFileInformation(File f) { if (f == null) return null; StringBuilder sb = new StringBuilder(); try { sb.append("\nf.getAbsoluteFile()="); sb.append(f.getAbsoluteFile()); sb.append("\nf.getAbsolutePath()="); sb.append(f.getAbsolutePath()); sb.append("\nf.getCanonicalFile()="); sb.append(f.getCanonicalFile()); sb.append("\nf.getCanonicalPath()="); sb.append(f.getCanonicalPath()); sb.append("\nf.getName()="); sb.append(f.getName()); sb.append("\nf.getParent()="); sb.append(f.getParent()); sb.append("\nf.getParentFile()="); sb.append(f.getParentFile()); sb.append("\nf.getPath()="); sb.append(f.getPath()); sb.append("\n--------------------------------------------"); sb.append("\nf.canRead()="); sb.append(f.canRead()); sb.append("\nf.canWrite()="); sb.append(f.canWrite()); sb.append("\nf.exists()="); sb.append(f.exists()); sb.append("\nf.isAbsolute()="); sb.append(f.isAbsolute()); sb.append("\nf.isDirectory()="); sb.append(f.isDirectory()); sb.append("\nf.isFile()="); sb.append(f.isFile()); sb.append("\nf.isHidden()="); sb.append(f.isHidden()); sb.append("\nf.lastModified()="); sb.append(f.lastModified()); sb.append("\nf.length()="); sb.append(f.length()); sb.append("\nf.toURI()="); sb.append(f.toURI()); sb.append("\nf.toURL()="); sb.append(f.toURL()); sb.append("\nf.pathSeparatorChar="); sb.append(f.pathSeparatorChar); sb.append("\nf.separatorChar="); sb.append(f.separatorChar); sb.append("\nf.pathSeparator="); sb.append(f.pathSeparator); } catch (Exception e) { throw new RuntimeException(e); } return sb.toString(); } // // ////////////////////////////////////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////////////////////////////////////// public static void stringToFile(String sourceString) { PrintWriter pw = null; try { JFileChooser jFileChooser = new JFileChooser(); javax.swing.filechooser.FileFilter fileFilter = new javax.swing.filechooser.FileFilter() { public boolean accept(File file) { if (file.isDirectory()) return true; String fileName = file.getName(); if (fileName.toUpperCase().endsWith("TXT")) return true; return false; } public String getDescription() { return "保存为文本文件"; } };// 建立一个过滤文件类型的过滤器(对话框中正确显示文件),是否启用过滤器决定于下面两句话 // jFileChooser.addChoosableFileFilter(fileFilter); // jFileChooser.setFileFilter(fileFilter); int returnValue = jFileChooser.showSaveDialog(null); File fileOfSave = null;// 保存文件句柄 if (returnValue == javax.swing.JFileChooser.APPROVE_OPTION) { fileOfSave = jFileChooser.getSelectedFile();// 获得文件句柄,文件是否存在还未知 } if(fileOfSave == null) return; String fileNameOfSave = fileOfSave.getName();// 返回输入的文件名 // 检查文件名是否符合要求,这一步暂时省略...................................... if (fileOfSave.exists() && !isAgree("该文件已经存在,确定要覆盖吗?")) return; else fileOfSave.createNewFile(); pw = new PrintWriter(fileOfSave); pw.print(sourceString); pw.flush(); } catch (IOException e) { throw new RuntimeException(e); } finally { if (pw != null) pw.close(); } } // ////////////////////////////////////////////////////////////////////////////////////////////////////// public static String fileToString() { StringBuilder sb = new StringBuilder(); JFileChooser jFileChooser = new JFileChooser(); javax.swing.filechooser.FileFilter fileFilter = new javax.swing.filechooser.FileFilter() { public boolean accept(File file) { if (file.isDirectory()) return true; String fileName = file.getName(); if (fileName.toUpperCase().endsWith("TXT")) return true; return false; } public String getDescription() { return "读取文本文件"; } }; // 建立一个过滤文件类型的过滤器(对话框中正确显示文件),是否启用过滤器决定于下面两句话 // jFileChooser.addChoosableFileFilter(fileFilter); // jFileChooser.setFileFilter(fileFilter); int returnValue = jFileChooser.showOpenDialog(null); if (returnValue == JFileChooser.APPROVE_OPTION) { File file = jFileChooser.getSelectedFile(); if (!file.exists()) { prompt("文件不存在"); return null; } // 检查文件名是否符合要求,这一步暂时省略...................................... BufferedReader br = null; try { br = new BufferedReader(new FileReader(file)); String string; while ((string = br.readLine()) != null) { sb.append(string); sb.append("\r\n"); } } catch (IOException e) { throw new RuntimeException(e); } finally { if (br != null) try { br.close(); } catch (IOException e) { throw new RuntimeException(e); } } } return sb.toString(); } public static boolean isAgree(String hint) { int returnValue = javax.swing.JOptionPane.showConfirmDialog(null, hint); if (returnValue == javax.swing.JOptionPane.YES_OPTION) return true; else return false; } public static int prompt(String promptMessage) { return JOptionPane.showConfirmDialog(null, promptMessage, "友情提示", JOptionPane.WARNING_MESSAGE); } // ///////////////////////////////////////////////////////////////////////////////////////////////////////
[解决办法]
///////////////////////////////////////////////////////////////////////////////////////////////////////// private static <T> void writeObjectToFile(T t, String fileName) {// 将一个对象 写入文件 if (t == null) return;// 空对象默认不写进去 ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(new FileOutputStream(fileName)); oos.writeObject(t); oos.flush(); oos.writeObject(null);// //写入结束标志方便读取(如果不写入,在读取的时候无法定位读取结束); oos.flush(); } catch (Exception e) { } finally { try { if (oos != null) oos.close(); } catch (Exception e) { } } } private static <T> T readObjectFromFile(String fileName) {// 从文件中读出一个对象 ObjectInputStream ois = null; try { ois = new ObjectInputStream(new FileInputStream(fileName)); T t = (T) ois.readObject(); return t; } catch (Exception e) { } finally { try { if (ois != null) ois.close(); } catch (Exception e) { } } return null; } ///////////////////////////////////////////////////////////////////////////////////////////////////// private static <T> void writeToFile(Collection<T> collection, String fileName) {// 将集合中的对象 写入文件 ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(new FileOutputStream(fileName)); Iterator<T> iterator = collection.iterator(); while (iterator.hasNext()) { T t = (T) iterator.next(); oos.writeObject(t); oos.flush(); } oos.writeObject(null);// //写入结束标志方便读取(如果不写入,在读取的时候无法定位读取结束); oos.flush(); } catch (Exception e) { } finally { try { if (oos != null) oos.close(); } catch (Exception e) { } } } private static <T> Collection readFromFile(String fileName) {// 从文件中读取对象 // 返回一个集合 ObjectInputStream ois = null; Collection<T> collection = new ArrayList<T>(); try { ois = new ObjectInputStream(new FileInputStream(fileName)); Object tempObject = null; T t = null; while ((tempObject = ois.readObject()) != null) { t = (T) tempObject; collection.add(t); } return collection; } catch (Exception e) { } finally { try { if (ois != null) ois.close(); } catch (Exception e) { } } return null; }
[解决办法]
上次发了一个简易向控制台打印输出的类 现在找不到了 重发一遍吧
//简便的打印方法: println() print() printf()//操作步骤///////////////////////////////////////////////////////////////////////////////////////1 .MyEclipse中编写类文件Print.java,内容为:package com.print;import java.io.PrintStream;public class Print { public static void print(Object obj) { System.out.print(obj); } public static void print(boolean b) { System.out.print(b); } public static void println(Object obj) { System.out.println(obj); } public static void println() { System.out.println(); } public static PrintStream printf(String format, Object... args) { return System.out.printf(format, args); } }///////////////////////////////////////////////////////////////////////////////////////2. 建立测试文件HelloWorld.java,内容为:package com.test;import com.print.*;//注意这行代码public class HelloWorld { public static void main(String[] args) { Print.println("HelloWorld");//引入形式不一样,操作语句不一样 Print.printf("%s%n","HelloWorld"); Print.print("HelloWorld"); }}///////////////////////////////////////////////////////////////////////////////////////上面的写法还是太长了,每次方法名前面写一个Print,那么采用下面的写法,我觉得会更加简洁//2. 建立测试文件HelloWorld.java,内容为:package com.test;import static com.print.Print.*;//注意这行代码,public class HelloWorld { public static void main(String[] args) { println("HelloWorld");//引入形式不一样,操作语句不一样 printf("%s%n","HelloWorld"); print("HelloWorld\n"); PrintStream ps = printf("%10s%n", "HelloWorld");//推荐使用%n而不是\n format("%10s%n", "HelloWorld");//推荐使用%n而不是\n }}
[解决办法]
JDK里面有 你可以参考下