发布于2021-06-12 15:27 阅读(206) 评论(0) 点赞(16) 收藏(4)
public class cest {
public static void main(String[] args){
System.out.println("程序开始执行");
method();
System.out.println("程序结束执行");
}
public static void method(){
try{
int a=10;
int b=0;
System.out.println(a/b);
}catch(ArithmeticException e){
//System.out.println("除数不能为0");
e.printStackTrace();
}
}
}
public class cest {
public static void main(String[] args) throws ParseException {
System.out.println("程序开始执行");
try{
method2();
}catch(ParseException e){
e.printStackTrace();
}
method1();
System.out.println("程序结束执行");
}
//运行时异常
public static void method1() throws ArithmeticException {
int a=10;
int b=5;
System.out.println(a/b);
//System.out.println("除数不能为0");
}
//编译时异常
public static void method2() throws ParseException{
String s = "2021-06-03";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d = sdf.parse(s);
System.out.println(d);
}
}
public class cest {
public static void main(String[] args){
System.out.println("程序开始执行");
method1();
method2();
System.out.println("程序结束执行");
}
//运行时异常
public static void method1(){
try{
int a=10;
int b=0;
System.out.println(a/b);
}catch(ArithmeticException e){
//System.out.println("除数不能为0");
e.printStackTrace();
}
//编译时异常
public static void method2() {
try {
String s = "2021-06-03";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d = sdf.parse(s);
System.out.println(d);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
}
public class cest {
public static void main(String[] args) throws ParseException {
//创建文件对象的三种方法
File f1 = new File("d:\\aa\\b.txt");
File f2 = new File("d:\\aa","b.txt");//目录,文件名
File f3 = new File("d:\\aa");
File f4 = new File(f3,"b.txt");
}
}
public class cest {
public static void main(String[] args) throws IOException {
File f1 = new File("d:\\aa\\b.txt");
System.out.println(f1.createNewFile());//创建文件
File f2= new File("d:\\bb");
System.out.println(f2.mkdir());//创建目录
File f3= new File("d:\\cc\\dd");
System.out.println(f3.mkdirs());//创建多级目录
File f4= new File("d:\\ee");
File f5= new File("d:\\ee\\f.txt");
System.out.println(f4.mkdir());
System.out.println(f5.createNewFile());//创建指定目录下的指定文件
}
}
public class cest {
public static void main(String[] args) throws IOException {
File f1 = new File("d:\\aa\\b.txt");
System.out.println(f1.createNewFile());//创建文件
System.out.println(f1.delete());//删除文件
File f2= new File("d:\\bb");
System.out.println(f2.mkdir());//创建目录
System.out.println(f2.delete());//删除目录
}
}
public class cest {
public static void main(String[] args) throws IOException {
File f1 = new File("aa\\b.txt");
System.out.println(f1.isDirectory());//判断是否是目录
System.out.println(f1.isFile());//判断是否是文件
System.out.println(f1.exists());//是否存在
System.out.println(f1.getAbsoluteFile());//获取绝对路径
System.out.println(f1.getPath());//获取相对路径
System.out.println(f1.getName());//获取名称
}
}
public class cest {
public static void main(String[] args) throws IOException {
FileOutputStream f1 = new FileOutputStream("b.txt");
//创建字节输出流对象做了以下三件事:
//调用系统功能创建文件
//创建字节输出流对象
//让f1指向b.txt文件
f1.write(65);
f1.close();
}
}
public class cest {
public static void main(String[] args) throws IOException {
FileOutputStream f1 = new FileOutputStream("b.txt");
f1.write(65);//写一个字节
byte[] by= {66,67,68,69};//写一个字节数组
f1.write(by);
byte[] bys="BCDE".getBytes();
f1.write(bys);//写一个字符串
f1.write("ABCDE".getBytes(),0,2);//写字符串的部分数据
f1.close();
}
}
public class cest {
public static void main(String[] args) throws IOException {
FileOutputStream f1 = new FileOutputStream("b.txt",true);//第二个参数为true,可以追加写入
for(int i=0;i<10;i++){
f1.write("ABCDE".getBytes());
f1.write("\n".getBytes());//换行
}
f1.close();
}
}
public class cest {
public static void main(String[] args) throws IOException {
FileInputStream f1 = new FileInputStream("b.txt");
int by=f1.read();
System.out.println(by);//读一个字
int ay;
while((ay=f1.read())!=-1){
System.out.println((char)ay);//读取多个字
}
f1.close();
}
}
public class cest {
public static void main(String[] args) throws IOException {
FileInputStream f1 = new FileInputStream("b.txt");
byte[] by= new byte[1024];
int len;
while((len=f1.read())!=-1){
System.out.println(new String(by,0,len));
}
f1.close();
}
}
原文链接:https://blog.csdn.net/gtt683559/article/details/117530992
作者:忽明忽灭
链接:http://www.javaheidong.com/blog/article/222532/c89e3c9bd61ea67b683b/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!