十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
在D盘新建两个文件test.txt,test1.txt
为苏尼特右等地区用户提供了全套网页设计制作服务,及苏尼特右网站建设行业解决方案。主营业务为成都做网站、成都网站设计、苏尼特右网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
把内容拷到test中,test1为输出。。。
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
public class ZhiDao {
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedReader br = null;
String lineContent = null;
StringBuffer sb = new StringBuffer();
PrintWriter pw = null;
try {
br = new BufferedReader(new FileReader("D:\\test.txt"));
pw = new PrintWriter("D:\\test1.txt");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
while ((lineContent = br.readLine()) != null) {
if(lineContent.equals("")){
sb.append("\r\n");
sb.append("\r\n");
}else{
for(int i = 0; ilineContent.length(); i++){
char charContent = lineContent.charAt(i);
sb.append(charContent);
if(i != 0 i%60 == 0){
sb.append("\r\n");
}
}
}
}
System.out.println(sb);
pw.write(sb.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
br.close();
pw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class OutToTxt {
private static BufferedWriter writer;
private static Scanner sc;
public static void main(String[] args) {
File out = new File("./Out.txt");
if(!out.exists()) {
try {
out.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
writer = new BufferedWriter(new FileWriter(out));
} catch (IOException e) {
e.printStackTrace();
}
sc = new Scanner(System.in);
System.out.println("请输入文本内容,输入exit结束:");
try {
writer.write(""); // 清空文本
String split = "";
while(true) {
String line = sc.nextLine();
if(line.equalsIgnoreCase("exit")) {
break;
}
writer.append(split + line);
split = "\r\n";
}
} catch (IOException e1) {
e1.printStackTrace();
} finally {
if(null != writer) {
try {
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
你可以运行后输入
Hello World!
This is my first application.
exit
写文件
import java.io.FileNotFoundException;
import java.io.FileOutputStream;//写文件
public class WriteFile {
public static void main(String[] args) throws Exception {
String temp="Hello world!\n";
FileOutputStream fos=new
FileOutputStream("D:\\GUI\\write.txt",true);//两个参数,true表示在文件末尾追加
fos.write(temp.getBytes());
fos.close();//流要及时关闭
}
}
读文件
import java.io.FileInputStream;//读文件
public class ReadFile {
public static void main(String[] args) throws Exception {
FileInputStream fis=new FileInputStream("D:\\GUI\\test.txt");
byte[] b=new byte[1024];
String result="";
while(true){
int num=fis.read(b);//num返回实际读到的字节数,如果文件读完了返回-1;
if(num==-1)break;
result=result+new String(b,0,num);
}
fis.close();
System.out.println(result);
}
}
输入无需使用字节流,直接字符流读取即可。
private void input(String fileName) throws IOException {
try(BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
String line;
while((line=reader.readLine()) != null) {
System.out.println(line);
}
}
}
同样输出,只要把Input换成Output;
private void output(String fileName, String content) throws IOException{
try(BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
writer.write(content);
writer.flush();
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
public class TestBaiduKnow {
public static void main(String[] args) throws IOException {
FileOutputStream fs = new FileOutputStream(new File("D:\\text.txt"));
PrintStream p = new PrintStream(fs);
p.println(100);
p.close();
}
}
//简单的一个例子,来模拟输出