Java基础知识学习2

1.文件的输入输出。scanner in = new scaner(new File("myfile.txt"));当如果是绝对地址时,每个\前都要加一个\. 写入文件 PrintWriter out = new PrintWriter("myfile.txt")


Scanner in  = new Scanner(FileRead("path"));
while(in.hasnext)
{
System.out.print(in.nextLine());
}

2.文件写入。PrintWriter使用后要close。要不然写的数据不会存进去。
        PrintWriter pw = new PrintWriter("c:\\test.txt");
        pw.write("marry wyp!happiness");
        pw.close();
        Scanner in = new Scanner(new FileReader("c:\\test.txt"));
        while(in.hasNext())
        {
            System.out.println(in.nextLine());
        }

3.文件创建。
  File f = new FIle("PATH")
f.createNewFile();
4.循环控制。gob bless me
        System.out.print("Do you love me? wyp(yes or no)");
        Scanner in = new Scanner(System.in);
        String str = in.next();
        while(!str.equalsIgnoreCase("yes"))
        {
            System.out.println("please answer again");
            str = in.next();
        }
        if(str.equalsIgnoreCase("yes"))
        {
            System.out.println("right,marry me!");
        }

5.switch中只能使用枚举类型或者整型。不能用字符串等!

6. 数组。for each 遍历数组拷贝。 Array.copyOf(array,length); 或者System.arrayCopy(from, fromIdex, to, toIndex, count);

7.IO流操作!
public static void main(String[] args) {
        try {
            // TODO code application logic here
            FileInputStream fis = new FileInputStream("c:\\test.txt");
            RandomAccessFile raf = new RandomAccessFile("e:\\forever.txt", "r");
            int flag = 0;
            try {
                while((flag = raf.read())!= -1 )
                {
                    System.out.print((char)flag);
                }
            } catch (IOException ex) {
                Logger.getLogger(FileIO.class.getName()).log(Level.SEVERE, null, ex);
            }
//            FileOutputStream fos = new FileOutputStream("e:\\forever.txt");
//            System.out.println("file content is:");
            int b;
            try {
                while((b = fis.read()) != -1)
                {
//                    fos.write(b);
                    System.out.print((char) b );
                }
//                fos.close();
            } catch (IOException ex) {
                Logger.getLogger(FileIO.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(FileIO.class.getName()).log(Level.SEVERE, null, ex);
        }

       
    }

8. 未初始化的对象不是对象。如 Date() birthday ; 如果调用birthday.toString();会报错!必须初始化之后才能使用。

9.当一个方法不需要访问对象状态。其所需的参数都是通过显示参数提供。或者一个方法需要访问类的静态域时。将该方法声明为静态方法。

你可能感兴趣的:(java基础)