博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
File Operations In Java
阅读量:6983 次
发布时间:2019-06-27

本文共 2544 字,大约阅读时间需要 8 分钟。

hot3.png

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 FILEFolder

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:

 //0123 import 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 51 First 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

你可能感兴趣的文章
【搜索引擎基础知识2】网络爬虫
查看>>
Aptana Studio 3 汉化
查看>>
phonegap+jquerymobile开发android的心得(4)
查看>>
python 使用PyTesser--安装
查看>>
无需编译,1分钟安装Ubuntu官方构建的最新版Linux内核
查看>>
解压即用,Ubuntu上Nginx/Apache/PHP编译打包
查看>>
table设置border没有空隙
查看>>
Maven的setting.xml 配置详解
查看>>
Python3.7源码在windows(VS2015)下的编译和安装
查看>>
10_css选择符类型1.html
查看>>
修改 liteide 的 godoc 文档样式
查看>>
Java学习笔记(35)——Java集合07之TreeMap
查看>>
甲骨文推Oracle WebLogic应用服务器12c
查看>>
WEB服务器、应用程序服务器、HTTP服务器区别
查看>>
工厂方法
查看>>
IPSEC ××× 综合应用
查看>>
Linux下安装及管理应用程序
查看>>
Vmware vCenter 配置分布式交换机
查看>>
Ubuntu下RabbitVCS的安装和简单使用
查看>>
scan-tcedit-user.bat
查看>>