1.

import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;

public class createFile {
public static void main(String[] args) {

}
//方法1
@Test
public void create01(){

String pathname ="D://陈志烨20215220608文件一.txt";

File file = new File(pathname);

try {
file.createNewFile();
System.out.println("创建文件一成功");
} catch (IOException e) {
throw new RuntimeException(e);
}


}
@Test
//方法二
public void create02(){
// String pathname="D://";
File parentFile=new File("D://");

String fileName="陈志烨20215220608文件二.txt";

File file=new File(parentFile,fileName);

try {
file.createNewFile();
System.out.println("创建文件二成功");
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Test
public void create03(){
String parentPath="D://";
String filePath="陈志烨20215220608文件三.txt";

File file=new File(parentPath,filePath);

try {
file.createNewFile();
System.out.println("创建文件三成功");
} catch (IOException e) {
throw new RuntimeException(e);
}
}


}

 

 

 

 

 

 

 

 

 

 

 

 2.

import org.testng.annotations.Test;

import java.io.File;

public class FileInformation {
public static void main(String[] args) {

}
//获取文件信息
@Test
public void Info(){
//先创建文件对象
File file=new File("D://陈志烨20215220608文件一.txt");

//调用相应方法,得到对应信息
System.out.println("文件名称:"+file.getName());

System.out.println("文件绝对路径:"+file.getAbsolutePath());

System.out.println("文件父目录:"+file.getParent());

System.out.println("文件大小(字节)"+file.length());

System.out.println("文件是否存在:"+file.exists());

System.out.println("是否是文件:"+file.isFile());

System.out.println("是否是目录:"+file.isDirectory());
}

}

 

 

3.

import org.testng.annotations.Test;

import java.io.File;

public class fileDirectory {
public static void main(String[] args) {

}
@Test
//删除文件
public void fileDelete(){
String filePath="D://陈志烨20215220608文件一.txt";
File file=new File(filePath);
if (file.exists()){
if (file.delete()){
System.out.println(filePath+"删除成功");
}else {
System.out.println(filePath+"删除失败");
}
}else {
System.out.println("文件不存在");
}
}
//输出目录
@Test
public void fileDeleteD(){
String filePath="D://陈志烨20215220608.txt";
File file=new File(filePath);
if (file.exists()){
if (file.delete()){
System.out.println(filePath+"删除成功");
}else {
System.out.println(filePath+"删除失败");
}
}else {
System.out.println("目录不存在");
}


}

//判断目录是否存在,不存在就创建
@Test
public void fileDeleteD1(){
String dirPath="D://text//陈志烨20215220608dir1.txt";
File file=new File(dirPath);
if (file.exists()){
System.out.println(dirPath+"该目录已经存在");
}else {
if (file.mkdir()){
System.out.println("创建成功");
}else {
System.out.println("创建失败");
}
}
}

}

 

 

 

 

 

 

 

 

4.

import java.util.Scanner;

public class scanPrintTest {
public static void main(String[] args) {
//创建Scanner对象,接受从控制台输入
Scanner input =new Scanner(System.in);
//接受String类型
String str =input.next();
//输出结果
System.out.println(str);

System.out.println("hellow world");
}
}

 

 

 

import java.util.Scanner;

public class scanTest {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
//double类型的数据
System.out.println("请输入一个double类型的数");
double d=input.nextDouble();
System.out.println(d);
//int类型的数据
System.out.println("请输入一个int类型的数");
int i=input.nextInt();
System.out.println(i);
//字符串类型的数据
System.out.println("请输入一个string类型的数");
String s=input.next();
System.out.println(s);


}
}

 

 

 5.

import org.testng.annotations.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class fileInputStream {
public static void main(String[] args) {

}
@Test
public void fileIn1(){
String filePath="D://陈志烨20215220608file.txt";
int readData;
FileInputStream fileInputStream=null;

try {
//创建FileInputStream对象,用于读取文件
fileInputStream=new FileInputStream(filePath);

while ((readData=fileInputStream.read())!=-1){
System.out.println((char)readData);
}
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
//关闭文件流
try {
fileInputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
@Test
public void fileIn2(){
String filePath="D://陈志烨20215220608file.txt";
int readData;
int readlength=0;
//字节数组
byte[] buf =new byte[8];
FileInputStream fileInputStream=null;
try {
//创建FileInputStream对象,用于读取文件
fileInputStream=new FileInputStream(filePath);

while ((readlength=fileInputStream.read(buf))!=-1){
System.out.println(new String(buf,0,readlength));
}

} catch (IOException e) {
throw new RuntimeException(e);
}finally {
//关闭文件流
try {
fileInputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
@Test
public void fileIn3(){
String filePath="D://陈志烨20215220608file.txt";
int readData;
int readlength=0;
//字节数组
byte[] buf =new byte[8];
FileInputStream fileInputStream=null;
try {
//创建FileInputStream对象,用于读取文件
fileInputStream=new FileInputStream(filePath);
while ((readlength=fileInputStream.read(buf))!=-1){
System.out.println(new String(buf,0,readlength));
}
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
//关闭文件流
try {
fileInputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

}

}

 

 

 

 

6.

import org.testng.annotations.Test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class outputStream {
public static void main(String[] args) {

}
//使用FileOutputStream将数据写到文件中
//如果文件不存在,则创建文件
@Test
public void writeFile(){
String filePath="D://陈志烨20215220608fileout.txt";
//创建对象
FileOutputStream fileOutputStream=null;
try {
fileOutputStream=new FileOutputStream(filePath);

//写入一个字节
//fileOutputStream.write('H');

//写入一个字符串
// String str="hello socket";
// str.getBytes()//将字符串转换为字节数组
// fileOutputStream.write(str.getBytes());

//写入字符串
String str ="hello lst";
fileOutputStream.write(str.getBytes(),0,str.length());

} catch (IOException e) {
throw new RuntimeException(e);
}finally {
try {
fileOutputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}

 

 

7.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class copyPic {
//file读和写实现复制文件
public static void main(String[] args)throws Exception {
//创建file对象
File f=new File("D://test.jpg");
//判断文件是否存在
if (f.exists()){
System.out.println("test.jpg存在,可以复制");
}else {
f.createNewFile();
System.out.println("test.jpg不存在,新建成功,可以复制");
}
//创建FileInputStream对象
FileInputStream inp=new FileInputStream(f);


File f1=new File("d://demo");
if (f1.isDirectory()){
FileOutputStream out=new FileOutputStream("d://demo//"+f.getName());
byte bytes[]=new byte[1024];
int temp=0;
while ((temp=inp.read(bytes))!=-1){
out.write(bytes,0,temp);
}
//结束
inp.close();
out.close();
System.out.println("文件拷贝成功");
}else {
f1.mkdir();
System.out.println("demo目录不存在,已经创建成功,继续复制");
FileOutputStream out=new FileOutputStream(":d://demo//"+f.getName());
byte bytes[]=new byte[1024];
int temp=0;
while ((temp=inp.read(bytes))!=-1){
out.write(bytes,0,temp);
}
//结束
inp.close();
out.close();
System.out.println("文件拷贝成功");


}

}
}

 

 

 

原文地址:http://www.cnblogs.com/chenzhiye1845/p/16859235.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性