This is the 28th day of my participation in the August Text Challenge.More challenges in August

Java uses FileReader and FileWriter to read and write files

FileReader and FileWriter read and write files.

Specific process:

// File path String path = Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"bz_Contact"+File.separator+"bz_contact.txt";Copy the code

FileWriter.java

// Construct a FileWriter object from the given File object. If the second argument is true, bytes are written to the end of the file instead of the beginning.

FileWriter writer = new FileWriter(file,true); // Write class BufferWriter bw = new BufferWriter(writer); Writer ("XXXXXXXXXXXXXXXXX"); // Write method bw.writer("XXXXXXXXXXXXXXXXX"); // flush the pipe bw.flush(); Bw.close ();Copy the code

Read FileReader. Java

Fr = new FileReader(file); StringBuffer sb = new StringBuffer(); // Read tool BufferReader reader = new BufferReader(fr); String line = reader.readerline (); // read while(line! = null){// Append sb.appen(line); // readerLine = reader.readerline (); } // Flush the pipe reader.flush(); Reader.close (); Buffer = new InputStreamReader(inputStream, "UTF-8 "); String str = buffer.readLine(); System.out.println(str); buffer.close();Copy the code

If you want to clear all the data in the folder

FileWriter = new FileWriter(file); writer.write(""); writer.flush(); writer.close(); FileInputStream inputStream = new FileInputStream(file); Int size = inputStream.available(); int size = inputStream.available(); Log.e(" clear cache "," clear cache "+size);Copy the code

This is to determine whether there is data in the file, if there is data on the first to clear, after the new

public void saveHtml(String html){
    try { 
        if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {    
        String tag = Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"news.html";
            File file = new File(tag);
            if(!file.exists()){
                file.createNewFile();
            }
            FileInputStream inputStream = new FileInputStream(file);
            if(inputStream.available()>0){
                FileWriter writer = new FileWriter(file); BufferedWriter bufferedWriter = new BufferedWriter(writer);
                bufferedWriter.write("");
                bufferedWriter.flush();
                bufferedWriter.close();
                writer.close();
                writer = null;
            }
            FileOutputStream outputStream = new FileOutputStream(file);
            FileWriter writer = new FileWriter(file);
            BufferedWriter bufferedWriter = new BufferedWriter(writer);
            bufferedWriter.write(html);
            bufferedWriter.flush();
            bufferedWriter.close();
            writer.close();
            writer = null;
        }
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
}
Copy the code

2. Several ways to get a mobile phone screen

1、
WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
int width = wm.getDefaultDisplay().getWidth();
int height = wm.getDefaultDisplay().getHeight();
Copy the code
2, WindowManager wm = this.getwinDowManager (); int width = wm.getDefaultDisplay().getWidth(); int height = wm.getDefaultDisplay().getHeight();Copy the code
3、
DisplayMetrics  dm = new DisplayMetrics();     
getWindowManager().getDefaultDisplay().getMetrics(dm);     
intscreenWidth = dm.widthPixels;    
intscreenHeight = dm.heightPixels;
Copy the code

3. The difference between LinkedHashMap and Map and several ways to traverse a Map

  • public class LinkedHashMap<K,V>extends HashMap<K,V>implements Map<K,V>

  • Hash table and linked list implementations of the Map interface, with a predictable iteration order. This implementation differs from HashMap in that the latter maintains a double-linked list running on all items. This list of links defines the iteration order, which is typically the order in which the keys are inserted into the map (insertion order). Note that if you reinsert the keys in the map, the insertion order is not affected. (If m.containsKey(k) returns true before calling m.put(k, v), the call will reinsert the key K into the map M.)

  • Four ways to traverse a Map

public static void main(String[] args) { Map map = new HashMap(); map.put("1", "value1"); map.put("2", "value2"); map.put("3", "value3"); Println (" Map. KeySet traverses key and value: "); for (String key : map.keySet()) { System.out.println("key= "+ key + " and value= " + map.get(key)); } // The second system.out.println (" iterator over keys and values via map.entryset: "); Iterator> it = map.entrySet().iterator(); while (it.hasNext()) { Map.Entry entry = it.next(); System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue()); } system.out.println (" traversal key and value through map.entrySet "); for (Map.Entry entry : map.entrySet()) { System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue()); } // system.out.println (" Map. Values () traverses all values but not keys "); for (String v : map.values()) { System.out.println("value= " + v); }}Copy the code