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. 将指定的form表单所有输入项转为json数据

    今天学习时,看到的将form表单中的输入数据转成json 的jquery代码,直接贴出来: $.fn.serializeJson=function(){ var serializeObj={}; va ...

  2. 对mysql快速批量修改,查重

    更新UPDATE mytable SET myfield = CASE id WHEN 1 THEN 'value' WHEN 2 THEN 'value' WHEN 3 THEN 'value' E ...

  3. 32-2题:LeetCode102. Binary Tree Level Order Traversal二叉树层次遍历/分行从上到下打印二叉树

    题目 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 ...

  4. Mysql5.7自定义函数递归报错1424 Recursive stored functions and triggers are not allowed

    示例: DELIMITER $$CREATE FUNCTION test(countnum INT)RETURNS INT DETERMINISTICBEGINDECLARE tempnum INT ...

  5. (77)zabbix主动、被动检测的详细过程与区别

    zabbix agent检测分为主动(agent active)和被动(agent)两种形式,主动与被动的说法均是相对于agent来讨论的.简单说明一下主动与被动的区别如下: 主动:agent请求se ...

  6. python 进度条 打印

  7. http 实战练习

    http 实战练习 建立httpd服务器,要求提供两个基于名称的虚拟主机: (1)www.X.com,页面文件目录为/web/vhosts/x:错误日志为/var/log/httpd/x.err,访问 ...

  8. nuxt.js服务端缓存lru-cache

    对于部分网页进行服务端的缓存,可以获得更好的渲染性能,但是缓存又涉及到一个数据的及时性的问题,所以在及时性和性能之间要有平衡和取舍. 官方文档里面写的使用方法 按照这个配置,试过了没什么用,但是从文档 ...

  9. composer 类加载器,对 <PSR-4的风格>、<PSR-0的风格>、<PEAR的风格> 风格的类的加载

    class ClassLoader { // ... /** * composer 类加载器,对 <PSR-4的风格>.<PSR-0的风格>.<PEAR的风格> 风 ...

  10. Ubuntu12.04下YouCompleteMe安装教程(部分)

    1.通过源码编译安装VIM 开发中使用的是Ubuntu 12.04 LTS,通过sudo apt-get install vim安装的版本较低,不支持YCM,所以,用源码编译并安装最新的Vim. 卸载 ...