public class Display {
目前创新互联已为近千家的企业提供了网站建设、域名、虚拟空间、网站运营、企业网站设计、宜阳网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
//定义ing型变量a
public int a ;
//获取a的值
public int getA() {
return a;
}
//设置a的值
public void setA(int a) {
this.a = a;
}
public static void main(String[] args){
Display dis = new Display();//定义一个对象dis
dis.setA(10);//为dis对象的成员变量a赋值为10
System.out.println("输出int型变量a的值:" + dis.getA());
}
}
public class HelloWord{
//自定义的输出函数,打印字符串helloword!
public void printHelloWord() {
System.out.println("helloword!");
}
public static void main(String args[]){
//创建对象实例
HelloWord hw = new HelloWord();
//通过对象实例调用自己的成员函数
hw.printHelloWord() ;
}
}
你看看满意不?
帮你写好了.
public class AAA {
public static void main (String[] args){
double PI, y;
PI = 4 * Math.atan(1.0);
y = F(0.5*PI);
System.out.println(y);
}
public static double F (double x){
return Math.sin(x);
}
}
/**
* @version 1.0
* @author 韩卫召
*
* 多种方式读文件的内容
* 按字节读取文件内容,按字符读取文件的内容,按行读取文件的内容,随即读取文件的内容
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.io.Reader;
import java.util.ResourceBundle;
public class ReadFromFile {
/**
* 以字节为单位读取文件的内容,常用于二进制文件,如声音,图象,影象等文件
*
* @param filename
* 文件名
*/
public static void readFileByBytes(java.lang.String filename) {
File file = new File(filename);
InputStream in = null;
System.out.println("以字节为单位读取文件的内容,一次读一个字节: ");
// 一次读一个字节
try {
in = new FileInputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int tempbyte;
try {
// 不断的读取,直到文件结束
while ((tempbyte = in.read()) != -1) {
System.out.write(tempbyte);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
System.out.println("以字节为单位读取文件内容,一次读多个字节: ");
// 一次读取多个字节
byte[] tempbytes = new byte[100];
int byteread = 0;
try {
in = new FileInputStream(filename);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ReadFromFile.showAvailabelBytes(in);
try {
while ((byteread = in.read(tempbytes)) != -1) {
// 读取多个字节到数组中,byteead为一次读取的字节数
System.out.write(tempbytes, 0, byteread);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void readFileByteChars(java.lang.String filename) {
/**
* 以字符为单位读取文件,常用与读文本,数字等类型的文件
*/
File file = new File(filename);
Reader reader = null;
System.out.println("以字符为单位读取文件内容,一次读一个字节: ");
// 一次读一个字符
try {
reader = new InputStreamReader(new FileInputStream(file));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
int tempchar;
try {
while ((tempchar = reader.read()) != -1) {
// 在Window下,\r\n这两个字符在一起时,表示一个换行
// 但如果这两个字符分开显示时,会换行两次行
// 因此,屏蔽掉\r,或者\n;否则,将会多出来很多空行
if (((char) tempchar) != '\r') {
System.out.println((char) tempchar);
}
}
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("以字符为单位读取文件内容,一次读多个字符: ");
char[] tempchars = new char[30];
int charread = 0;
try {
reader = new InputStreamReader(new FileInputStream(filename));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
// 读入多个字符到字符数组中,charread为一次读取字符数
while ((charread = reader.read(tempchars)) != -1) {
if ((charread == tempchars.length)
(tempchars[tempchars.length - 1] != '\r')) {
System.out.println(tempchars);
} else {
for (int i = 0; i charread; i++) {
if (tempchars[i] == '\r') {
continue;
} else {
System.out.print(tempchars[i]);
}
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
}
}
}
}
public static void readFileByLines(java.lang.String filename) {
File file = new File(filename);
BufferedReader reader = null;
// System.out.println("以行为单位读取文件的内容,一次读一整行: ");
try {
reader = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
java.lang.String tempString = null;
int line = 1;
try {
while ((tempString = reader.readLine()) != null) {
if(tempString.indexOf("福州")!=-1){
System.out.println("靠!出错了!赶紧报警啊! " + line + ": " + tempString);
}
// System.out.println("line " + line + ": " + tempString);
line++;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void readFileByRandomAccess(java.lang.String filename) {
RandomAccessFile randomFile = null;
System.out.println("随即读取一段文件内容: ");
try {
randomFile = new RandomAccessFile(filename, "r");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long fileLength = 0;
try {
fileLength = randomFile.length();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int beginIndex = (fileLength 4) ? 4 : 0;
try {
randomFile.seek(beginIndex);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] bytes = new byte[10];
int byteread = 0;
try {
while ((byteread = randomFile.read(bytes)) != -1) {
System.out.write(bytes, 0, byteread);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (randomFile != null) {
try {
randomFile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
private static void showAvailabelBytes(InputStream in) {
try {
System.out.println("当前字节输入流中的字节数为: " + in.available());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(java.lang.String args[]) {
ResourceBundle rb = ResourceBundle.getBundle("PreventTampering");
String filePath = rb.getString("filePath");
java.lang.String filename = filePath+"test.txt";
System.out.println(filename);
// ReadFromFile.readFileByBytes(filename);
// ReadFromFile.readFileByteChars(filename);
ReadFromFile.readFileByLines(filename);
// ReadFromFile.readFileByRandomAccess(filename);
}
Java的一些常用函数,方法总结:
1:数组方面:
数组是一个对象,数组名可以理解和C++里面一样,
1):System.arraycopy(src,srcindex,dst,dstindex,src.length);
2):java.util.Arrays.sort(数组名);//块数排序法进行排序
3):java.util.Arrays.binarySearch(arr,key);//从哪里收,关键值,返回索引.没有的话返回-1
4):java.util.Array.fill(arr,value),设置数组初始值
5):Array.equals(arr1,arr2)//比较两个数组中元素是不是全部相等,是返回true,不是返回false
2:时间方面:
public static String dateToStr(java.util.Date date)
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = sdf.format(date);
return str;
}
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class DateUtil
{
public static java.util.Date parseDate(String dateStr, String format)
{
java.util.Date date = null;
try
{
java.text.DateFormat df = new java.text.SimpleDateFormat(format);
String dt = dateStr.replaceAll("-", "/");
if ((!dt.equals("")) (dt.length() format.length()))
{
dt += format.substring(dt.length()).replaceAll("[YyMmDdHhSs]", "0");
}
date = (java.util.Date) df.parse(dt);
}
catch (Exception e)
{
}
return date;
}
public static java.util.Date parseDate(String dateStr)
{
return parseDate(dateStr, "yyyy/MM/dd");
}
public static String format(java.util.Date date, String format)
{
String result = "";
try
{
if (date != null)
{
java.text.DateFormat df = new java.text.SimpleDateFormat(format);
result = df.format(date);
}
}
catch (Exception e)
{
}
return result;
}
public static String format(java.util.Date date)
{
return format(date, "yyyy/MM/dd");
}
public static int getYear(java.util.Date date)
{
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.get(java.util.Calendar.YEAR);
}
public static int getMonth(java.util.Date date)
{
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.get(java.util.Calendar.MONTH) + 1;
}
public static int getDay(java.util.Date date)
{
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.get(java.util.Calendar.DAY_OF_MONTH);
}
public static int getHour(java.util.Date date)
{
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.get(java.util.Calendar.HOUR_OF_DAY);
}
public static int getMinute(java.util.Date date)
{
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.get(java.util.Calendar.MINUTE);
}
public static int getSecond(java.util.Date date)
{
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.get(java.util.Calendar.SECOND);
}
public static long getMillis(java.util.Date date)
{
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.getTimeInMillis();
}
public static String getDate(java.util.Date date)
{
return format(date, "yyyy/MM/dd");
}
public static String getTime(java.util.Date date)
{
return format(date, "HH:mm:ss");
}
public static String getDateTime(java.util.Date date)
{
return format(date, "yyyy/MM/dd HH:mm:ss");
}
public static java.util.Date addDate(java.util.Date date, int day)
{
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTimeInMillis(getMillis(date) + ((long) day) * 24 * 3600 * 1000);
return c.getTime();
}
public static int diffDate(java.util.Date date, java.util.Date date1)
{
return (int) ((getMillis(date) - getMillis(date1)) / (24 * 3600 * 1000));
}
public static String getMonthBegin(String strdate)
{
java.util.Date date = parseDate(strdate);
return format(date, "yyyy-MM") + "-01";
}
public static String getMonthEnd(String strdate)
{
java.util.Date date = parseDate(getMonthBegin(strdate));
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, 1);
calendar.add(Calendar.DAY_OF_YEAR, -1);
return formatDate(calendar.getTime());
}
public static String formatDate(java.util.Date date)
{
return formatDateByFormat(date, "yyyy-MM-dd");
}
public static String formatDateByFormat(java.util.Date date, String format)
{
String result = "";
if (date != null)
{
try
{
SimpleDateFormat sdf = new SimpleDateFormat(format);
result = sdf.format(date);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
return result;
}
}
Java日期格式化及其使用例子
1 SimpleDateFormat担当重任,怎样格式化都行
import java.util.Date;
import java.text.SimpleDateFormat;
public class Demo
{
public static void main(String[] args)
{
Date now=new Date();
SimpleDateFormat f=newSimpleDateFormat("今天是"+"yyyy年MM月dd日 E kk点mm分");
System.out.println(f.format(now));
f=new SimpleDateFormat("a hh点mm分ss秒");
System.out.println(f.format(now));
}
}
2 从字符串到日期类型的转换:
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.text.*;
publicclass Demo
{
public static void main(String[] args)
{
String strDate="2005年04月22日";
//注意:SimpleDateFormat构造函数的样式与strDate的样式必须相符
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy年MM月dd日");
//必须捕获异常
try
{
Date date=simpleDateFormat.parse(strDate);
System.out.println(date);
}
catch(ParseException px)
{
px.printStackTrace();
}
}
}
3 将毫秒数换转成日期类型
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.text.*;
public class Demo
{
public static void main(String[] args)
{
long now=System.currentTimeMillis();
System.out.println("毫秒数:"+now);
Date dNow=new Date(now);
System.out.println("日期类型:"+dNow);
}
}
4 获取系统时期和时间,转换成SQL格式后更新到数据库
java.util.Date d=new java.util.Date(); //获取当前系统的时间
//格式化日期
new java.text.SimpleDateFormat s= new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = s.format(d); //转为字符串
使用RS更新数据库,仍然要用rs.updateString,而不是rs.updateDade。
rs.updateString("regtime",dateStr); //regtime字段为datetime类型的
5 按本地时区输出当前日期
Date myDate = new Date();
System.out.println(myDate.toLocaleString());
输出结果为:
2003-5-30
6 如何格式化小数
DecimalFormat df = new DecimalFormat(",###.00");
double aNumber = 33665448856.6568975;
String result = df.format(aNumber);
Sytem. out.println(result);
输出结果为:
33,665,448,856.66
其他:获取毫秒时间 System.currentTimeMillis();
7 在数据库里的日期只以年-月-日的方式输出
定义日期格式:SimpleDateFormat sdf = new SimpleDateFormat(yy-MM-dd);
sql语句为:String sqlStr = "select bookDate from roomBook where bookDate between '2007-4-10' and '2007-4-25'";
输出:
System.out.println(df.format(rs.getDate("bookDate")));