写程序判断d盘根目录下a.txt文件是否存在,如果存在则向其内依次写入字符串"will smith
写程序判断d盘根目录下a.txt文件是否存在,如果存在则向其内依次写入字符串"willsmith",整数25,写入后再从文件中将其读出。(用DataOutputStrea...
写程序判断d盘根目录下a.txt文件是否存在,如果存在则向其内依次写入字符串"will smith",整数25,写入后再从文件中将其读出。(用DataOutputStream,DataInputStream)
展开
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励20(财富值+成长值)
1个回答
展开全部
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
public class TestZhihu {
public static void main(String[] args) {
/*boolean flag = true;
int n = 0;
int t = 0;
while(flag){
t += n+1;
if(t>=8888){
flag = false;
}
n++;
}
System.out.println("===="+n);*/
try {
writeFile("d:\\a.txt","will smith 25");
System.out.println(readFile("d:\\a.txt"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String readFile(String path) throws IOException{
File file = new File(path);
//输入流
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader reader = null;
StringBuffer resultBuffer = new StringBuffer();
String tempLine = null;
try {
inputStream = new FileInputStream(file);
inputStreamReader = new InputStreamReader(inputStream);
reader = new BufferedReader(inputStreamReader);
while ((tempLine = reader.readLine()) != null) {
resultBuffer.append(tempLine);
}
System.out.println(resultBuffer.toString());
}finally{
if (reader != null) {
reader.close();
}
if (inputStreamReader != null) {
inputStreamReader.close();
}
if (inputStream != null) {
inputStream.close();
}
}
return resultBuffer.toString();
}
public static void writeFile(String path,String inputValue) throws IOException{
//TODO 实现文件的读写判断
File file = new File(path);
//输出流
OutputStream outputStream = null;
OutputStreamWriter outputStreamWriter = null;
try {
outputStream = new FileOutputStream(file);
outputStreamWriter = new OutputStreamWriter(outputStream);
outputStreamWriter.write(inputValue);
outputStreamWriter.flush();
}finally{
if (outputStreamWriter != null) {
outputStreamWriter.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
public class TestZhihu {
public static void main(String[] args) {
/*boolean flag = true;
int n = 0;
int t = 0;
while(flag){
t += n+1;
if(t>=8888){
flag = false;
}
n++;
}
System.out.println("===="+n);*/
try {
writeFile("d:\\a.txt","will smith 25");
System.out.println(readFile("d:\\a.txt"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String readFile(String path) throws IOException{
File file = new File(path);
//输入流
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader reader = null;
StringBuffer resultBuffer = new StringBuffer();
String tempLine = null;
try {
inputStream = new FileInputStream(file);
inputStreamReader = new InputStreamReader(inputStream);
reader = new BufferedReader(inputStreamReader);
while ((tempLine = reader.readLine()) != null) {
resultBuffer.append(tempLine);
}
System.out.println(resultBuffer.toString());
}finally{
if (reader != null) {
reader.close();
}
if (inputStreamReader != null) {
inputStreamReader.close();
}
if (inputStream != null) {
inputStream.close();
}
}
return resultBuffer.toString();
}
public static void writeFile(String path,String inputValue) throws IOException{
//TODO 实现文件的读写判断
File file = new File(path);
//输出流
OutputStream outputStream = null;
OutputStreamWriter outputStreamWriter = null;
try {
outputStream = new FileOutputStream(file);
outputStreamWriter = new OutputStreamWriter(outputStream);
outputStreamWriter.write(inputValue);
outputStreamWriter.flush();
}finally{
if (outputStreamWriter != null) {
outputStreamWriter.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询