本文目錄一覽:
- 1、java 通過字元串獲取文件名
- 2、java 字元串存為文件
- 3、java 用系統時間做文件的保存名稱
- 4、java 字元串(文件名),得到文件格式字元串
- 5、java輸出系統時刻並且輸入到一個文件里
java 通過字元串獲取文件名
如果unistat/ 文件都在這個路徑下可以寫為
subString(url.indexOf(」unistat/「)+8,url.indexOf(」.jsp「)+4);
java 字元串存為文件
很簡單啊,new一個文件,然後寫入新建的文件中,新建的文件名後綴用doc就行了
java 用系統時間做文件的保存名稱
SimpleDateFormat si=new SimpleDateFormat(“yyyy-MM-dd hh:mm:ss”);
///獲得當前系統時間 年-月-日 時:分:秒
String time=si.format(new Date());
//將時間拼接在文件名上即可
java 字元串(文件名),得到文件格式字元串
public class FindType {
public static void main(String[] args) {
String s1 = “20151012.mp4”;
System.out.println(getType(s1));//.mp4
String s2 = “123.007.txt”;//假設文件名有很多的點也可以
System.out.println(getType(s2));//.txt
}
//方法去尋找最後一個點開頭的字元串
static String getType(String s){
String[] ss = s.split(“\\.”);//表示用.去切割字元串
return “.”+ss[ss.length-1];//用.連接最後一個字元串
}
}
輸出
.mp4
.txt
java輸出系統時刻並且輸入到一個文件里
Date now = new Date();
SimpleDateFormat format=new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
String text = format.format(now);
/**
* 將字元串寫到文件中去
* @param fileName 文件名
* @param text 文本
*/
public static void write(String fileName, String text) {
try {
PrintWriter out = new PrintWriter(new File(fileName).getAbsoluteFile());
try {
out.print(text);
} finally {
out.close();
}
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/220077.html