import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter; public class FileIO { /**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException { // 1.创建文件夹 mkdir
// 如果路径存在,才可以通过该方法创建文件夹。。。mkdirs路径不存在也可以通过这个方法创建,先创建路径再创建文件夹
File myFolderPath = new File("D:\\MyEclipse2013Work\\Test\\test");
try {
if (!myFolderPath.exists()) {
myFolderPath.mkdir();
}
} catch (Exception e) {
System.out.println("新建目录操作出错");
e.printStackTrace();
} // 2. 创建文件 FileWrite 和PrintWrite 的区别需要注意
File myFilePathFile = new File("test.txt");
try {
if (!myFilePathFile.exists()) {
myFilePathFile.createNewFile();
}
FileWriter resultFile = new FileWriter(myFilePathFile);
PrintWriter myFile = new PrintWriter(resultFile);
myFile.println("新建文件操作");
myFile.println("新建文件操作");
myFile.println("新建文件操作");
myFile.println("新建文件操作");
myFile.write("sss", 1, 1);
myFile.println("新建文件操作");
resultFile.write("sssssssssssss");
resultFile.close();
} catch (Exception e) {
// TODO: handle exception
System.out.println("新建文件操作出错");
e.printStackTrace();
} // 3. FileWrite 结合BufferedWriter打印文本换行
TestBufferedWriter(); // 4.获得当前工程的路径
File directory = new File("");// 参数为空
String courseFile = directory.getCanonicalPath();
System.out.println(courseFile);
} public static void TestBufferedWriter() {
try { String filePath = "test1.txt";
File myFile = new File(filePath);
if (!myFile.exists()) {
myFile.createNewFile();
}
FileWriter resultFile = new FileWriter(myFile);
BufferedWriter buffer = new BufferedWriter(resultFile);
String fileContent = "This is my name./n we are isoftsotne.com /n chinesebillboy at here。/n";
String contentString = "";
int flag = 0;
boolean isGO = true;
while ((flag = fileContent.indexOf("/n")) != -1 && isGO) {
contentString = fileContent.substring(0, flag);
buffer.write(contentString);
buffer.newLine();
buffer.flush();
if (flag + 2 >= fileContent.length()) {
isGO = false;
} else {
fileContent = fileContent.substring(flag + 2);
}
}
buffer.flush();
resultFile.flush();
buffer.close();
resultFile.close();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally { }
}
}

  

java写文件的基本操作的更多相关文章

  1. java写文件实现换行

    Java 写文件实现换行   第一种: 写入的内容中利用\r\n进行换行 File file = new File("D:/text"); try { if(!file.exist ...

  2. java写文件时,输出不完整的原因以及解决方法

    在java的IO体系中,写文件通常会用到下面语句 BufferedWriter bo=new BufferedWriter(new FileWriter("sql语句.txt")) ...

  3. java 写文件解析

    import java.io.File; import java.io.FileOutputStream; import java.io.*; public class FileTest { publ ...

  4. java写文件时,输出不完整的原因以及解决方法close()或flush()

    在java的IO体系中,写文件通常会用到下面语句 BufferedWriter bw=new BufferedWriter(new FileWriter("sql语句.txt")) ...

  5. java写文件时往末尾追加文件(而不是覆盖原文件),的两种方法总结

    代码如下: import java.io.FileWriter; import java.io.IOException; import java.io.RandomAccessFile; public ...

  6. java写文件

                                  randomAccessFile.close();              }                  e.printStack ...

  7. java对文件的基本操作

    package cn.edu.fhj.day009.FileDemo; import java.io.File; import java.io.IOException; public class Fi ...

  8. java写文件读写操作(IO流,字符流)

    package copyfile; import java.io.*; public class copy { public static void main(String[] args) throw ...

  9. java写文件读写操作(IO流,字节流)

    package copyfile; import java.io.*; public class copy { public static void main(String[] args) throw ...

随机推荐

  1. sql注入问题 java中将MySQL的数据库验证秘密加上 ' or '1'= '1 就可以出现万能密码

    password的字符串中,加上 ' or '1'= '1 就可以制作出万能密码. 原因如下: 原代码中密码是123456 执行数据库查询语句 实际上执行的SQL语句是: select * from ...

  2. linux下避免僵尸进程的几种方法

    linux下我们可以调用fork函数创建子进程,创建的子进程将会得到父进程的数据空间.堆.栈......副本(采用写时复制机制),子进程将会继承父进程的信号掩码.信号处理方式.当前工作目录.会话id. ...

  3. 【思维题 经典模型】cf632F. Magic Matrix

    非常妙的经典模型转化啊…… You're given a matrix A of size n × n. Let's call the matrix with nonnegative elements ...

  4. HDU 2045 LELE的RPG难题

    递推 枚举起点状态 #include <algorithm> #include <iostream> #include <cstring> #include < ...

  5. nginx负载均衡核心组件介绍

    一.nginx upstream 模块介绍 1.upstream模块介绍 nginx的负载均衡功能依赖于ngx_http_upstream_module模块,所支持的代理方式包括 proxy_pass ...

  6. TP5 发送邮件代码

    发送邮箱邮件方法 /** * 系统邮件发送函数 * @param string $tomail 接收邮件者邮箱 * @param string $name 接收邮件者名称 * @param strin ...

  7. 原生js替换jQuery各种方法-中文版

    原文https://github.com/nefe/You-D... 原生JS与jQuery操作DOM对比 You Don't Need jQuery 前端发展很快,现代浏览器原生 API 已经足够好 ...

  8. Codeforces Round #443 (Div. 2) C 位运算

    C. Short Program time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  9. Postgres安装详解

    PG安装 一.基础包的安装(yum源的配置,可以采用光盘挂载,及ftp yum源,针对外网环境忽略此步): yum -y install wget tcpdump glibc libgcc gcc g ...

  10. 2017 ACM-ICPC EC-Final ShangHai(思维乱搞赛)

    感觉全是思维乱搞题. Gym - 101775J Straight Master 给你n种扑克,你每次可以出连续的3 ~ 5 张,问你能否出完. Sample Input 2 13 1 2 2 1 0 ...