Cary's blog
Jan 1, 2035
Feb 26, 2018
[JAVA] 讀取檔案 與 輸出
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class ReadFile {
public static void main(String[] args) {
System.out.println(">>>>> hello world... >>>>>");
try( // JDK7 AutoClose
//輸入來源為檔案
BufferedReader br = new BufferedReader(new FileReader(new File("檔案路徑")));
//輸出至Console
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
) {
char[] buffer = new char[1024];
int readCnt = 0; //讀取筆數
while ((readCnt = br.read(buffer)) != -1) {
System.out.println("========================================================");
bw.write(buffer, 0, readCnt);
bw.flush();
System.out.println("");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Labels:
JAVA
Feb 22, 2018
[SCJP 6.0] ch10 輸入輸出 筆記
RAM <= 輸入:開檔,打字
RAM => 輸出:存檔,顯示螢幕
java.io.
Console:
Console c = System.console();
%...格式字串
String: readLine()
char[]: 密碼
File: 檔案 or 目錄 (基本資訊)
File f = new File("路徑");
真正建立檔案/目錄:
f.createNewFile();
f.mkdirs();
讀取檔案"內容": (close())
byte:
InputStream => FileInputStream(File).read();
讀取1byte(int),用(char)轉型 read檔尾-1
OutputStream => FileOutputStream(File).write(byte[]);
byte[] => String.getBytes()
InputStream => ObjectInputStream(InputStream).readObject();
檔尾EOFException
OutputStream => ObjectOutputStream(OutputStream).writeObject(Object);
Object => Serializable
不想序列化的屬性:transient (直接套用預設值)
字元:
Reader => InputStreamReader => FileReader(File).read();
read檔尾-1
Writer => OutputStreamWriter => FileWriter(File).write(String);
Reader => BufferedReader(Reader).readLine();
null: 讀完
Writer => BufferedWriter(Writer).write(String);
.flush(); 強制寫檔,刷新緩衝區
new BufferedReader( new FileReader( new File(路徑) ) )
new ObjectOutputStream( new FileOutputStream( new File(路徑) ) )
也有 BufferedInputStream(InputStream).read()
new PrintWriter(BufferedWriter).printf(String format, Object... args)
RAM => 輸出:存檔,顯示螢幕
java.io.
Console:
Console c = System.console();
%...格式字串
String: readLine()
char[]: 密碼
File: 檔案 or 目錄 (基本資訊)
File f = new File("路徑");
真正建立檔案/目錄:
f.createNewFile();
f.mkdirs();
讀取檔案"內容": (close())
byte:
InputStream => FileInputStream(File).read();
讀取1byte(int),用(char)轉型 read檔尾-1
OutputStream => FileOutputStream(File).write(byte[]);
byte[] => String.getBytes()
InputStream => ObjectInputStream(InputStream).readObject();
檔尾EOFException
OutputStream => ObjectOutputStream(OutputStream).writeObject(Object);
Object => Serializable
不想序列化的屬性:transient (直接套用預設值)
字元:
Reader => InputStreamReader => FileReader(File).read();
read檔尾-1
Writer => OutputStreamWriter => FileWriter(File).write(String);
Reader => BufferedReader(Reader).readLine();
null: 讀完
Writer => BufferedWriter(Writer).write(String);
.flush(); 強制寫檔,刷新緩衝區
new BufferedReader( new FileReader( new File(路徑) ) )
new ObjectOutputStream( new FileOutputStream( new File(路徑) ) )
也有 BufferedInputStream(InputStream).read()
new PrintWriter(BufferedWriter).printf(String format, Object... args)
Labels:
JAVA
Jan 9, 2013
JAVA日期處裡
簡單來說,如果要設定日期的話利用Calendar來處理
取今天日期用Date就可
至於日期格式就要利用SimpleDateFormat來處理了
取今天日期用Date就可
至於日期格式就要利用SimpleDateFormat來處理了
package edu.test;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Test{
public static void main(String args[]){
//假設要處理民國101年2月的字串"10102", 取得西元年份與月份
int year = Integer.parseInt("10102".substring(0, 3))+1911;
int month = Integer.parseInt("10102".substring(3, 5))-1;
Calendar cal = Calendar.getInstance();
//end:截止日期為"10102"的最後一日
Date end = null;
cal.set(year,month, 01);
cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DAY_OF_MONTH));
end = cal.getTime();
System.out.println("偷印看看cal長怎樣 => "+cal);
//beg:起始日期為"10102"減五個月的最後一日
Date beg = null;
cal.add(Calendar.MONTH,-5);
cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DAY_OF_MONTH));
beg = cal.getTime();
System.out.println("偷印看看cal長怎樣 => "+cal);
System.out.println("西元年(Date) => "+beg+", "+end);
//Date轉String, 並利用SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
System.out.println("西元年(String) => "+sdf.format(beg)+", "+sdf.format(end));
//now:現在日期
Date now = new Date();
System.out.println("現在日期西元年(String) => "+sdf.format(now));
}
}
OUTPUT:
偷印看看cal長怎樣 => java.util.GregorianCalendar[time=1330522727243,areFieldsSet=true,areAllFieldsSet=false,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Taipei",offset=28800000,dstSavings=0,useDaylight=false,transitions=42,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2012,MONTH=1,WEEK_OF_YEAR=9,WEEK_OF_MONTH=5,DAY_OF_MONTH=29,DAY_OF_YEAR=60,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=5,AM_PM=1,HOUR=9,HOUR_OF_DAY=21,MINUTE=38,SECOND=47,MILLISECOND=243,ZONE_OFFSET=28800000,DST_OFFSET=0] 偷印看看cal長怎樣 => java.util.GregorianCalendar[time=1317389927243,areFieldsSet=true,areAllFieldsSet=false,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Taipei",offset=28800000,dstSavings=0,useDaylight=false,transitions=42,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2011,MONTH=8,WEEK_OF_YEAR=40,WEEK_OF_MONTH=5,DAY_OF_MONTH=30,DAY_OF_YEAR=273,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=5,AM_PM=1,HOUR=9,HOUR_OF_DAY=21,MINUTE=38,SECOND=47,MILLISECOND=243,ZONE_OFFSET=28800000,DST_OFFSET=0] 西元年(Date) => Fri Sep 30 21:38:47 CST 2011, Wed Feb 29 21:38:47 CST 2012 西元年(String) => 2011/09/30, 2012/02/29 現在日期西元年(String) => 2013/01/09
Labels:
JAVA
Jan 8, 2013
Jan 7, 2013
String, List 轉JSON與output格式
This code try to create JSONObject and JSONArray by String and List.
package edu.test;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
public class Test{
public static void main(String args[]){
//Create JSONObject by String
String jsonObjString1 = "{\"id\": \"12345\", \"value\": \"Good Job!\"}";
System.out.println("jsonObjString1 = "+jsonObjString1);
JSONObject jsonObj1 = (JSONObject) JSONSerializer.toJSON(jsonObjString1);
System.out.println("jsonObj1 = "+jsonObj1);
//Create JSONObject by put key and value
JSONObject jsonObj2 = new JSONObject();
jsonObj2.put("id", "67890");
jsonObj2.put("value", "Oh Ya!");
System.out.println("jsonObj2 = "+jsonObj2);
//Create JSONArray by JSONObject
JSONArray jsonArr1 = new JSONArray();
jsonArr1.add(jsonObj1);
jsonArr1.add(jsonObj2);
System.out.println("jsonArr1 = "+jsonArr1);
//Create JSONArray by List
List<JSONObject> jsonObjList = new ArrayList<JSONObject>();
jsonObjList.add(jsonObj1);
jsonObjList.add(jsonObj2);
System.out.println("jsonObjList = "+jsonObjList);
JSONArray jsonArr2 = (JSONArray) JSONSerializer.toJSON(jsonObjList);
System.out.println("jsonArr2 = "+jsonArr2);
//Create a JSONArray List
List<JSONArray> jsonArrList = new ArrayList<JSONArray>();
jsonArrList.add(jsonArr1);
jsonArrList.add(jsonArr2);
System.out.println("jsonArrList = "+jsonArrList);
}
}
OUTPUT:
jsonObjString1 = {"id": "12345", "value": "Good Job!"}
jsonObj1 = {"id":"12345","value":"Good Job!"}
jsonObj2 = {"id":"67890","value":"Oh Ya!"}
jsonArr1 = [{"id":"12345","value":"Good Job!"},{"id":"67890","value":"Oh Ya!"}]
jsonObjList = [{"id":"12345","value":"Good Job!"}, {"id":"67890","value":"Oh Ya!"}]
jsonArr2 = [{"id":"12345","value":"Good Job!"},{"id":"67890","value":"Oh Ya!"}]
jsonArrList = [[{"id":"12345","value":"Good Job!"},{"id":"67890","value":"Oh Ya!"}], [{"id":"12345","value":"Good Job!"},{"id":"67890","value":"Oh Ya!"}]]
Labels:
JAVA
Subscribe to:
Comments (Atom)