⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 输入和输出技术.txt

📁 适合初学者练习 包括awt小程序、list和map群体等100多个java程序
💻 TXT
📖 第 1 页 / 共 2 页
字号:
输入和输出技术 List.1 ReadLine/ReadLine.java 
输入和输出技术 List.2 Directory/Directory.java 
输入和输出技术 List.3 FileInfo/FileInfo.java 
输入和输出技术 List.4 FilterDir/FilterDir.java 
输入和输出技术 List.5 CopyFile/CopyFile.java 
输入和输出技术 List.6 ReadText/ReadText.java 
输入和输出技术 List.7 WriteData/WriteData.java 
输入和输出技术 List.8 ReadData/ReadData.java 
输入和输出技术 List.9 WriteText/WriteText.java 
输入和输出技术 List.10 ReadRandom/ReadRandom.java 

--------------------------------------------------------------------------------

输入和输出技术 List.1 ReadLine/ReadLine.java 
Return to top 
001: import java.io.*;
002: 
003: public class ReadLine {
004: 
005:  // Input a string
006:  public static String readLine()
007:   throws IOException {
008:   BufferedReader br = 
009:    new BufferedReader(new InputStreamReader(System.in));
010:   return br.readLine();
011:  }
012: 
013:  // Prompt for and input a string
014:  public static String readLine(String prompt)
015:   throws IOException {
016:   System.out.print(prompt);
017:   return readLine();
018:  }
019: 
020:  // Main program
021:  public static void main(String args[])
022:   throws IOException {
023:   System.out.println("Enter strings when prompted.");
024:   System.out.print("Enter your first name: ");
025:   String s1 = readLine();
026:   String s2 = readLine("Enter your last name: ");
027:   System.out.println("Your name: " + s1 + " " + s2);
028:  }
029: }

Return to top 
--------------------------------------------------------------------------------

输入和输出技术 List.2 Directory/Directory.java 
Return to top 
001: import java.io.*;
002: 
003: public class Directory {
004: 
005:  // Input a string
006:  public static String readLine()
007:   throws IOException {
008:   BufferedReader br = 
009:    new BufferedReader(new InputStreamReader(System.in));
010:   return br.readLine();
011:  }
012: 
013:  // Prompt for and input a string
014:  public static String readLine(String prompt)
015:   throws IOException {
016:   System.out.print(prompt);
017:   return readLine();
018:  }
019: 
020:  // Construct File object for directory path
021:  public static File getFileForPath(String path)
022:   throws IOException {
023:   File dir = new File(path);
024:   if (!dir.isDirectory())
025:    throw new IOException("Not a directory");
026:   return dir;
027:  }
028: 
029:  // Main program method
030:  public static void main(String args[]) {
031:   String path;
032:   try {
033:    // Get pathname from command line or prompt user
034:    if (args.length > 0)
035:     path = args[0];
036:    else
037:     path = readLine("Path name? ");
038:   // List directory
039:    File dir = getFileForPath(path);
040:    String[] filenames = dir.list();
041:    for (int i = 0; i < filenames.length; i++)
042:     System.out.println(filenames[i]);
043:   } catch (IOException e) {            // Trap exception
044:    System.err.println(e.toString());   // Display error
045:   }
046:  }
047: }

Return to top 
--------------------------------------------------------------------------------

输入和输出技术 List.3 FileInfo/FileInfo.java 
Return to top 
001: import java.io.*;
002: import java.util.Date;
003: 
004: public class FileInfo {
005: 
006:  // Input a string
007:  public static String readLine()
008:   throws IOException {
009:   BufferedReader br = 
010:    new BufferedReader(new InputStreamReader(System.in));
011:   return br.readLine();
012:  }
013: 
014:  // Prompt for and input a string
015:  public static String readLine(String prompt)
016:   throws IOException {
017:   System.out.print(prompt);
018:   return readLine();
019:  }
020: 
021:  // Construct File object for named file
022:  public static File getFileForFilename(String filename)
023:   throws IOException {
024:   File fi = new File(filename);
025:   // Do not move the following statements;
026:   // order is critical
027:   if (!fi.exists())
028:    throw new IOException("File not found");
029:   if (!fi.isFile())
030:    throw new IOException("Not a file");
031:   return fi;
032:  }
033: 
034:  // Show a labeled string
035:  public static void showLabel(String label, String s) {
036:   System.out.print(label);
037:   System.out.println(s);
038:  }
039: 
040:  // Display information about file fi
041:  public static void showInformation(File fi) {
042:   showLabel("Path     = ", fi.getPath());
043:   showLabel("Filename = ", fi.getName());
044:   showLabel("Length   = ",
045:    new Long(fi.length()).toString());
046:   showLabel("Readable = ", 
047:    new Boolean(fi.canRead()).toString());
048:   showLabel("Writable = ", 
049:    new Boolean(fi.canWrite()).toString());
050:   showLabel("Modified = ",
051:    new Date(fi.lastModified()).toString());
052:  }
053: 
054:  // Main program method
055:  public static void main(String args[]) {
056:   String filename;
057:   try {
058:    // Get pathname from command line or prompt user
059:    if (args.length > 0)
060:     filename = args[0];
061:    else
062:     filename = readLine("File name? ");
063:    File fi = getFileForFilename(filename);
064:    showInformation(fi);
065:   } catch (IOException e) {            // Trap exception
066:    System.err.println(e.toString());   // Display error
067:   }
068:  }
069: }

Return to top 
--------------------------------------------------------------------------------

输入和输出技术 List.4 FilterDir/FilterDir.java 
Return to top 
001: import java.io.*;
002: 
003: // File filter class
004: class FilterClass implements FilenameFilter {
005:  public boolean accept(File dir, String name) {
006:   File f = new File(dir, name);
007:   if (f.isDirectory())
008:    return true;
009:   else
010:    return false;
011:  }
012: }
013: 
014: public class FilterDir {
015: 
016:  // Input a string
017:  public static String readLine()
018:   throws IOException {
019:   BufferedReader br = 
020:    new BufferedReader(new InputStreamReader(System.in));
021:   return br.readLine();
022:  }
023: 
024:  // Prompt for and input a string
025:  public static String readLine(String prompt)
026:   throws IOException {
027:   System.out.print(prompt);
028:   return readLine();
029:  }
030: 
031:  // Construct File object for directory path
032:  public static File getFileForPath(String path)
033:   throws IOException {
034:   File dir = new File(path);
035:   if (!dir.isDirectory())
036:    throw new IOException("Not a directory");
037:   return dir;
038:  }
039: 
040:  // Main program method
041:  public static void main(String args[]) {
042:   String path;
043:   try {
044:    // Get pathname from command line or prompt user
045:    if (args.length > 0)
046:     path = args[0];
047:    else
048:     path = readLine("Path name? ");
049:    File dir = getFileForPath(path);
050:    String[] filenames = dir.list(new FilterClass());
051:    for (int i = 0; i < filenames.length; i++)
052:     System.out.println(" " + filenames[i]);
053:   } catch (IOException e) {            // Trap exception
054:    System.err.println(e.toString());   // Display error
055:   }
056:  }
057: }

Return to top 
--------------------------------------------------------------------------------

输入和输出技术 List.5 CopyFile/CopyFile.java 
Return to top 
001: import java.io.*;
002: 
003: public class CopyFile {
004:  
005:  // Input a string
006:  public static String readLine()
007:   throws IOException {
008:   BufferedReader br = 
009:    new BufferedReader(new InputStreamReader(System.in));
010:   return br.readLine();
011:  }
012: 
013:  // Prompt for and input a string
014:  public static String readLine(String prompt)
015:   throws IOException {
016:   System.out.print(prompt);
017:   return readLine();
018:  }
019: 
020:  // Construct File object for named file
021:  public static File getFileForFilename(
022:   String filename, boolean checkExistence)
023:   throws IOException {
024:   File fi = new File(filename);
025:   if (checkExistence) {
026:    // Do not move the following statements;
027:    // order is critical
028:    if (!fi.exists())
029:     throw new IOException(fi.getName() + " not found");
030:    if (!fi.isFile())
031:     throw new IOException(fi.getName() + " is not a file");
032:   }
033:   return fi;
034:  }
035:  
036:  // Returns true if user answers yes to prompt 
037:  public static boolean yes(String prompt)
038:   throws IOException {
039:   System.out.print(prompt);
040:   char ch = (char)System.in.read();
041:   if (ch == 'y' || ch == 'Y') {
042:    return true;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -