Java读文件

   public class FileServer {
       public static void main(String[] args) {
           try {
               File file = new File("data.txt");
               Scanner scanner = new Scanner(file);
               while (scanner.hasNextLine()) {
                   String s = scanner.nextLine();
                   System.out.println(s);
               }
               scanner.close();
           } catch (FileNotFoundException e) {
               System.out.println(e);
               e.printStackTrace();
           }
       }
   }
   public class GetFileInfo {
       public static void main(String[] args) {
           File file = new File("filename.txt");
           if (file.exists()) {
               System.out.println("File name: " + file.getName());
               System.out.println("Absolute path: " + file.getAbsolutePath());
               System.out.println("Writeable: " + file.canWrite());
               System.out.println("Readable: " + file.canRead());
               System.out.println("File size in bytes: " + file.length());
           } else{
               System.out.println("The file does not exist.");
           }
       }
   }

 

posted @ 2022-09-26 00:09  zjy4fun  阅读(14)  评论(0编辑  收藏  举报