File Operations In Java
2019独角兽企业重金招聘Python工程师标准>>>
The “File” class in Java defines many useful methods, here is a program which demonstrates some of these methods.
import java.io.*;public class streams
{public static void main(String []args){File f1=new File("Folder/FILE");File f2=new File("Folder/FILE1");String s;if(f1.exists()){if(f1.isFile()){System.out.println("File Name is "+f1.getName());s=f1.getParent();File f3=new File(s);f1.renameTo(new File("Folder/abc"));f2.delete();if (f3.isDirectory()){System.out.println(f2.getPath());}}else{System.out.println("Not a File");}}}
}
The output of the program is:File Name is FILE
Folder
If successfully run , the ” FILE ” file inside the folder ” Folder ” will be renamed to ” abc ” and the ” FILE1 ” file will be deleted.
Here is an example of a program that reads its own first six bytes, we have:
//0123import java.io.*;public class read
{public static void main(String []args){int s=6;int b[]=new int[6];char c[]=new char[6];try{FileInputStream f = new FileInputStream("read.java");for (int i=0; i<6; i++){b[i] = f.read();c[i] = (char) b[i];}System.out.println("First 6 bytes of the file are :");for (int i=0;i<6;i++)System.out.print(b[i]+" ");System.out.println("nnFirst 6 Bytes as characters :");for (int i=0;i<6;i++)System.out.print(c[i]);}catch (Exception e){System.out.println("Error");}}
}This program produces the following output:First 6 bytes of the file are :
47 47 48 49 50 51First 6 Bytes as characters are ://0123
Notice that the FileInputStream object is created inside a try-catch block since if the specified-file does not exist, an exception is raised.
In the same way to write data to a file byte-by-byte, we have:
import java.io.*;public class witer
{public static void main(String []args) throws IOException{String s="Hello";byte b[]=s.getBytes();FileOutputStream f=new FileOutputStream("file.txt");int i=0;while(i{f.write(b[i]);i++;}}
}
If the file called file.txt does not exist, it is automatically created.
If we place a true in the constructor for the FileOutputStream, then the file would be opened in append mode.
Note: All file paths used here are relative paths , Use absolute path or add the relative path to the classpath, Copy the folders to the bin folder
版权声明:本文为博主原创文章,未经博主允许不得转载。
转载于:https://my.oschina.net/xiaohelong/blog/497849
File Operations In Java相关推荐
- azure java_Azure File服务(5): Java开发
使用Java管理Azure文件共享服务 Azure文件共享服务提供了多种方式的访问接口,包括Powershell,.Net, Java, Python等等,本章主要介绍如何使用Java来访问Azure ...
- Invocation failed Unexpected end of file from server java.lang.RuntimeException: Invocation failed U
看下错误日志: Invocation failed Unexpected end of file from server java.lang.RuntimeException: Invocationf ...
- Wait Event Disk file operations I/O
ORALCE的等待事件有很多,今天在一个数据库的awr报告中发现了一个新的等待事件:Disk file operations I/O,官方解释如下: This event is used to wai ...
- ...is public, should be declared in a file named “ScresourcesApplic.java“---springcloud工作笔记164
Class 'ScresourcesApplication' is public, should be declared in a file named 'ScresourcesApplication ...
- 已解决:An error occurred at line: 1 in the generated java file The type java.io.ObjectInputStream canno
tomcat报错 An error occurred at line: 1 in the generated java file The type java.io.ObjectInputStream ...
- java file.separator_关于Java的File.separator
一.File类 在Windows下的路径分隔符(\)和在Linux下的路径分隔符(/)是不一样的,当直接使用绝对路径时,跨平台会报No Such file or diretory异常. File中还有 ...
- Selected class file name ‘Employee.java‘ mapped to not java file type ‘Files supported via TextMate
Unable to parse template "Class" Error message: Selected class file name 'Employee.java' m ...
- File类和Java I/O流备忘
一.File类 说明 java.io.File 类是文件和目录路径名的抽象表示,主要用于文件和目录的创建.查找和删除等操作. 构造方法 public File(String pathname) :通过 ...
- Unable to parse template Class Error message: Selected class file name 'HelloController.java' mapp
一.问题: Unable to parse template "Class" Error message: Selected class file name 'HelloContr ...
最新文章
- Go []byte to a C *char
- python入门指南百度云-Python入门指南
- windows socket 简单实例
- 笨方法python_笨方法学习Python(11-20)
- 直白介绍卷积神经网络(CNN)
- 分析Linux文件rwx属性的含义
- MyBatis 解决模糊查询包含特殊字符
- flutter tab选项卡appbar中的选项卡
- 数据结构_C语言_实验一_线性结构 ——一元多项式求导
- python 负数索引
- Condition实现线程通信
- 为什么会学习感觉痛苦
- APP自动化测试(一)-appium
- 淘宝开放平台API接口,接入方案如下
- git clone 多个_搭建自己的 Git 服务器
- 网络-01-端口号-linux端口详解大全
- Search For Free —— 新闻爬虫及爬取结果的查询网站
- 记一次gitbook的安装
- php 绘制商品海报
- html音乐播放器换歌,web-audio-api可视化音乐播放器,实现暂停切换歌曲功能,粉色系专场~...