标签:style blog io ar color os sp java 文件
字符串操作:
字符串分割:String array[] = str.split("\t");
列表操作:
定义:List<String> list=new ArrayList();
添加元素:list.add("apple");
判断是否包含某元素:list.contains("apple")
hashmap操作:
定义:
HashMap<String, Double> map = new HashMap<String, Double>();
map.put("apple", "1.0");
判断是否包含key:map.containsKey("apple")
获取key-value:map.get("apple")
JAVA读文件:
1.按行读取文件内容
package readFile; import java.io.BufferedReader; import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Main { public static void main(String[] args) throws FileNotFoundException,IOException{ FileReader reader = new FileReader("resource/test.txt"); //input path BufferedReader br = new BufferedReader(reader); String str = null; while((str = br1.readLine()) != null) { System.Out.println(str); }
br.close(); } }
JAVA 写文件
package writeFile; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class Main { public static void main(String[] args) throws FileNotFoundException,IOException{ BufferedWriter writer = new BufferedWriter(new FileWriter(new File("resource/wt.txt")));//write path
//FileWriter writer = new FileWriter("resource/wt.txt");//该方法也可以 writer.write("hello\tworld\r\n"); writer.close() } }
PS:若希望输入的新内容不覆盖文件已有的内容,则改为
BufferedWriter writer = new BufferedWriter(new FileWriter(new File("resource/wt.txt",true)));或
FileWriter writer = new FileWriter("resource/wt.txt",true);
标签:style blog io ar color os sp java 文件
原文地址:http://www.cnblogs.com/tec-vegetables/p/4135631.html