📄 4.11.txt
字号:
一、文件的读(是指往内存、CPU里读)
public class Input {
/**
*
* @param filePathAndName the name of file
* @return content of file
*/
public String readContent(String filePathAndName) {
//File myFile = new File("d:/TestJava/javaFile.txt");
File myFile = new File(filePathAndName);
FileInputStream fis = null;
// 定义一个字节数组,用于存储每次读取的内容
byte [] buff = new byte[1024];
// 定义一个int型数值,用于记录每次读取内容的长度
int n =0;
// 定义一个String对象,用于存储读取的全部内容
String content ="";
try{
//生成一个FileInputStream类的实例,用于读取文件内容。
fis = new FileInputStream(myFile);
/*
FileInputStream的read(byte [] b)方法每次读取的内容存储在字节数组b中
每次读取的长度最长为b.length. 返回值是读取内容的长度。如果读到文件尾,返回-1
*/
// 这是新手容易理解的写法
while(n != -1){
if(n !=0){
String temp =new String(buff,0,n);
content = content+temp;
}
n =fis.read(buff);
}
/*// 这是程序员的习惯写法
while((n = fis.read(buff)) != -1){
String temp =new String(buff,0,n);
content += temp;
}
*/
}catch (IOException e) {
e.printStackTrace();
}
finally{
try {
// 一定不要忘记关闭
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return content;
}
}
--------------------------------
public class TestInput {
public static void main(String[] args) {
Input in = new Input();
String cont = in.readContent("D:\\readme.txt");
System.out.println(cont);
}
}
二、文件的写(是指往硬盘里写 )
public class Output {
public void write(String fileName) {
FileOutputStream ou;
PrintStream ps;
try {
ou = new FileOutputStream(fileName);
/**
* 试一下,这两个有什么区别:
*
* ou = new FileOutputStream(fileName);
* ou = new FileOutputStream(fileName,true);
*
*/
ps = new PrintStream(ou);
ps.println("春江花月夜");
ps.println("春江潮水连海平");
ps.println("海上明月共潮生");
ps.println("潋滟随波千万里");
ps.println("何处春江无月明");
ps.println("江流宛转绕芳甸");
ps.println("月照花林皆似霰");
ps.println("空里流霜不觉飞");
ps.println("汀上白沙看不见");
ps.println("... ...");
ps.close();
ou.close();
System.out.println("文件写入成功");
}catch(Exception e) {
e.printStackTrace();
}
}
}
------------------------------
public class TestOutput {
public static void main(String[] args) {
Output o = new Output();
o.write("D:\\aa.txt");
}
}
三、文件的复制(两个文件)
public class CopyFile {
public void copy(String srcName, String desName) {
File src = new File(srcName);
File des = new File(desName);
FileInputStream fis = null;
FileOutputStream fos = null;
try {
des.createNewFile();
fis = new FileInputStream(src);
fos = new FileOutputStream(des);
int read;
byte [] buf = new byte[1024];
while((read = fis.read(buf)) != -1) {
fos.write(buf,0,read);
}
fos.flush();
fos.close();
fis.close();
System.out.println("文件copy成功");
}catch(Exception e) {
e.printStackTrace();
}
}
}
-------------------------------------------------
public class TestCopyFile {
public static void main(String[] args) {
CopyFile cf = new CopyFile();
cf.copy("D:\\readme.txt","D:\\aa.txt");
}
}
四、文件的操作
public class TestFile1 {
public static void main(String[] args) {
String path ="D:/TestJava/src";
File dir = new File(path);
//目录是否存在
boolean flg = dir.exists();
if(flg){
System.out.println("目录存在"+path);
}else{
System.out.println("目录不存在"+path);
}
// 是不是目录
flg = dir.isDirectory();
System.out.println(flg ? "是目录" : "不是目录");
// 是不是文件
flg = dir.isFile();
System.out.println(flg ? "是文件" : "不是文件");
//是不是绝对路径
flg = dir.isAbsolute();
System.out.println(flg ? "是绝对路径" : "不是绝对路径");
File adir = new File(path);
if(dir == adir){
System.out.println("dir == adir");
}else{
System.out.println("dir != adir");
}
// 目录名
System.out.println(dir.getName());
//路径
System.out.println(dir.getPath());
//绝对路径
System.out.println(dir.getAbsolutePath());
//上一级目录
System.out.println(dir.getParent());
//返回String 数组,对应目录下的文件和目录
String [] names = dir.list();
if(names != null){
for(int i =0; i< names.length ; i++){
System.out.print(names[i] + " ");
}
System.out.println("*****************");
}
//返回File数组,对应目录下的文件和目录
File [] dirorfile = dir.listFiles();
//文件长度
System.out.println(dir.length());
//最后修改时间
System.out.println(dir.lastModified());
// 返回目录下特定名称的文件
String [] javaFile = dir.list(new JavaFilter());
for(int i =0; i< javaFile.length ; i++){
System.out.print(javaFile[i] + " ");
}
}
}
========================================
public class JavaFilter implements FilenameFilter
{
public JavaFilter()
{
}
/**
*只接受java文件
*@see JavaList
*@param args Path,FileName
*@return true or false
*/
public boolean accept(File dir,String name)
{
return name.endsWith(".java");
}
}
---------------------------------------------------------------
public class TestFile {
public static void main(String[] args) {
String fileName = "d:/TestJava/javaFile.txt";
// 构造方法 参数是路径+文件名
File myFile = new File(fileName);
try {
//只有调用 createNewFile() 才真正在硬盘上生成文件
boolean flg = myFile.createNewFile();
System.out.println(flg ? "生成了文件"+"/"+ fileName :"没有生成文件");
} catch (IOException e) {
e.printStackTrace();
}
String dir = "d:/TestJava";
String name="aFile.txt";
// 构造方法 参数是路径+文件名
File yourFile = new File(dir,name);
try {
//只有调用 createNewFile() 才真正在硬盘上生成文件
boolean flg = yourFile.createNewFile();
System.out.println(flg ? "生成了文件"+dir+"/"+name :"没有生成文件");
} catch (IOException e) {
e.printStackTrace();
}
File myFile1 = new File(fileName);
if(myFile == myFile1){
System.out.println("myFile == myFile1");
}else{
System.out.println("myFile != myFile1");
}
// 文件名
System.out.println(myFile.getName());
}
}
-------------------------------------------------------------------
五、属性文件
public class ReadPro {
public String url = "";
public String name = "";
public ReadPro() {
try {
Properties props = new Properties();
//注意:目录分隔符号使用 \\ 或者 /
File f = new File("D:\\J1122\\iodemo\\src\\prop\\test.properties");
//File f = new File("c:/eclipse/workspace/Unit9/src/prop/test.properties");
FileInputStream in = new FileInputStream(f);
props.load(in);
in.close();
// 注意:属性文件不能使用中文
url = props.getProperty("url");
name=props.getProperty("name");
}catch(Exception e) {
e.printStackTrace();
}
}
}
---------------------
public class Test {
public static void main(String[] args) {
ReadPro rp = new ReadPro();
System.out.println(rp.url);
System.out.println(rp.name);
}
}
------------------------
Test.project文件
url=127.0.0.1;
name=tiger;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -