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. 表格和网页ico图标

    表格: 表格格式: <table> <tr> 表格的行 <th >表头</th> <th>表头 </th> </tr> ...

  2. centos 6 安装VMware Tools

    开启虚拟机的centos系统, 在虚拟机工具栏点击 “虚拟机”=>VMwareTools安装,  centos系统内的桌面会有一个VMware Tools的驱动光驱, 双击打开后,有一个tar. ...

  3. node基础

    javascript window gulp ---- 前端工程构建工具 webpack ---- 前端工程构建工具 java Python php:后台 本地电脑,服务器 node 本地或服务端运行 ...

  4. phpstorm —— Xdebug 的配置和使用

    给phpstorm 配置Xdebug(Xdebug 是 PHP 的一个扩展, 用于帮助调试和开发.它包含一个与 ide 一起使用的单步调试器.它升级了 PHP 的 var_dump () 功能) 这篇 ...

  5. 绘制矩形:描边矩形imagerectangle()、填充矩形imagefilledrectangle()

    <?php //1. 绘制图像资源(创建一个画布) $image = imagecreatetruecolor(500, 300); //2. 先分配一个绿色 $green = imagecol ...

  6. Base64及其Python实现

    1. 什么是Base64 Base64是一种基于64个可打印字符来表示二进制数据的表示方法 Base64是一种编码方式,提及编码方式,必然有其对应的字符集合.在Base64编码中,相互映射的两个集合是 ...

  7. ACM模板

    #include <iostream> //万能头文件#include<bits/stdc++.h> 方便时用 #include <algorithm> #incl ...

  8. 用 Tensorflow 建立 CNN

    稍稍乱入的CNN,本文依然是学习周莫烦视频的笔记. 还有 google 在 udacity 上的 CNN 教程. CNN(Convolutional Neural Networks) 卷积神经网络简单 ...

  9. [译]Exactly once is NOT exactly the same

    近日学习Pulsar文档时,注意到Pulsar提到其提供的是effectively-once语义,而不是其它流计算引擎announce的exactly-once语义,并引用了Exactly once ...

  10. UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 19: ordinal not in range(128)

    解决方案: 1: 在网上找到的解决方案是: 在调用import matplotlib.pyplot as plt前 import sys sys.setdefaultencoding(“gbk”) 让 ...