import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.FileWriter
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader
/**
* java读写文件
* 读取d:/1.txt文件内容,写入f:/text.txt文件中.
*
* 写入文件换行用fw.write("\r\n")
* 或者fw.write("\n")
* @author young
*
*/
public class FileWriterTest {
// 读写文件
public static void rwFile(){
FileWriter fw = null
BufferedReader br = null
try {
// 定义FileWriter对象,关联文件f:\text.txt,用来向文件写内容
fw = new FileWriter("f:\\text.txt", true)
// 定义bufferedReader对象,用来读取d:\1.txt文件内容
br = new BufferedReader(new InputStreamReader(
new FileInputStream("d:\\1.html"), "UTF-8"))
String line = null
// 每次读取一行内容,循环读取,读到文件末尾结束
while ((line = br.readLine()) != null) {
System.out.println("文件内容: " + line)
fw.write(line)
// 刷新缓冲流,
fw.flush()
}
// 关闭I/O流
br.close()
} catch (FileNotFoundException e) {
e.printStackTrace()
} catch (IOException e) {
e.printStackTrace()
} finally {
if (fw != null) {
try {
fw.close()
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
}
}
public static void main(String[] args) {
rwFile()
}
}
function jsonclick() {var jsonstr = '{"desc":"OK","status":1000,"data":{"wendu":"34","ganmao":"各项气象条件适宜,发生感冒机率较低。但请避免长期处于空调房间中,以防感冒。","forecast":[{"fengxiang":"东风","fengli":"3-4级","high":"高温 35℃","type":"多云","low":"低温 27℃","date":"22日星期一"},{"fengxiang":"东风","fengli":"微风级","high":"高温 34℃","type":"多云","low":"低温 26℃","date":"23日星期二"},{"fengxiang":"东风","fengli":"微风级","high":"高温 35℃","type":"多云","low":"低温 27℃","date":"24日星期三"},{"fengxiang":"东风","fengli":"微风级","high":"高温 34℃","type":"多云","low":"低温 26℃","date":"25日星期四"},{"fengxiang":"东北风","fengli":"3-4级","high":"高温 33℃","type":"阴","low":"低温 25℃","date":"26日星期五"}],"yesterday":{"fl":"3-4级","fx":"北风","high":"高温 33℃","type":"雷阵雨","low":"低温 27℃","date":"21日星期日"},"aqi":"53","city":"常州"}}'
var list = new Function("return" + jsonstr)()
//html代码随便拼凑成需要的格式就行了
var htmlstr = "<ul style='border:1px solid #aaccff height:100px width:600px'> <li> 状态码:"
+ list.status + "</li> <li> 日期:"
+ list.data.yesterday.date + "</li> <li> 地址:"
+ list.data.city + "</li> <li> 预测风向:"
+ list.data.forecast[0].fengxiang + "</li> </ul>"
document.getElementById("id").innerHTML = htmlstr
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)