import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Test6 {
public static String read(String filename) throws IOException{
BufferedReader in=new BufferedReader(new FileReader(filename));
String s;
StringBuilder stringbuilder=new StringBuilder();
while((s=in.readLine())!=null){
stringbuilder.append(s+"\n");
}
in.close();
return stringbuilder.toString();
}
public static void main(String[] args) throws IOException {
System.out.println(read("D:/Test.txt"));
}
}
private void writeTxt(HashMap<String, String> map) {
File file = new File("e:/Data.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
return;
}
}
OutputStreamWriter os = null;
try {
os = new OutputStreamWriter(new FileOutputStream(file, true));
for (String key : map.keySet()) {
String property = map.get(key);
os.write(property);
os.write("\t");
os.write(key);
os.write("\r\n");
}
os.write("==========");
os.write("\r\n");
os.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void writeExcel(HashMap<String, String> map){
File file=new File("E:/Test/Data.xls");
POIFSFileSystem fs;
try {
fs = new POIFSFileSystem(new FileInputStream(file));
FileOutputStream fileOut = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheet("sheet1");
for(String key:map.keySet()){
HSSFRow row = sheet.createRow((short)(sheet.getLastRowNum()+1));
HSSFCell pnCell1 = row.createCell(0);
HSSFCell pnCell2 = row.createCell(1);
String value=map.get(key);
pnCell1.setCellValue(value);
pnCell2.setCellValue(key);
}
wb.write(fileOut);
fileOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
java IO
最新推荐文章于 2023-02-03 00:07:13 发布