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("文件拷贝成功");
}
}
}
标签:截图,String,System,代码运行,println,new,public,out From: https://www.cnblogs.com/chenzhiye1845/p/16859235.html