Java Useful Program(1)

public static void main(String[] args){
//字符串有整型的相互转换
String str=String.valueOf(123);
int i=Integer.parseInt(str);
System.out.println(i);
//向文件末尾添加内容
BufferedWriter out = null;
try{
out = new BufferedWriter(new FileWriter("c://logs//log.out", true));
out.write("aString");
}catch(IOException ex){
System.out.println(ex.getMessage());
}finally{
if(out!=null){
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//得到当前方法的名字
String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
System.out.println(methodName);

//转字符串到日期
SimpleDateFormat format = new SimpleDateFormat( "dd.MM.yyyy" );
try {
Date date = format.parse("01.01.2013");
System.out.println(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

//把 Java util.Date 转成 sql.Date
java.util.Date utilDate = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
System.out.println(sqlDate);

//使用NIO进行快速的文件拷贝
File f1=new File("c://logs//log.out");
File f2=new File("c://logs//log1.out");
FileChannel inChannel=null;
FileChannel outChannel=null;
try {
inChannel = new FileInputStream(f1).getChannel();
outChannel = new FileOutputStream(f2).getChannel();
try{
int maxCount = (64 * 1024 * 1024) - (32 * 1024);
long size = inChannel.size();
long position = 0;
while ( position < size ){
position += inChannel.transferTo( position, maxCount, outChannel );

}
}catch(FileNotFoundException ex){
ex.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(inChannel!=null){
try {
inChannel.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(outChannel!=null){
try {
outChannel.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}

Output:

123
main
Tue Jan 01 00:00:00 CST 2013
2013-06-17

### Java Try-Catch-Finally Usage and Best Practices In Java, `try`, `catch`, and `finally` blocks provide a mechanism for handling exceptions gracefully without terminating the program abruptly. Understanding how each part works ensures robust error management within applications. #### Structure of Try-Catch-Finally Blocks A typical structure involves wrapping code that might throw an exception inside a `try` block followed by one or more `catch` clauses designed to handle specific types of exceptions thrown during execution: ```java try { // Code that may throw an exception } catch (SpecificException e) { // Handle SpecificException here } finally { // Cleanup actions go here } ``` The `finally` clause executes regardless of whether any preceding catches were triggered—useful for releasing resources like file handles or network connections[^1]. #### Demonstrating Exception Handling Below demonstrates proper utilization including multiple catches alongside resource cleanup via `finally`. This example also showcases catching both checked (`IOException`) and unchecked (`NullPointerException`) exceptions: ```java import java.io.*; public class Example { public static void main(String[] args) { BufferedReader br = null; try { String pathToFile = "example.txt"; br = new BufferedReader(new FileReader(pathToFile)); System.out.println(br.readLine()); } catch (FileNotFoundException ex) { System.err.println("File not found: " + ex.getMessage()); } catch (IOException | NullPointerException ex) { System.err.println("An I/O Error occurred: " + ex.getMessage()); } finally { if (br != null) { try { br.close(); } catch (IOException ex) { System.err.println("Failed to close reader"); } } } } } ``` This snippet illustrates best practices when dealing with potential errors while ensuring all allocated resources get properly released even upon encountering issues[^2].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值