网站建设资讯

NEWS

网站建设资讯

java的代码函数 java语言代码

java中读字符的函数代码!

/**

成都创新互联公司,为您提供重庆网站建设成都网站制作、网站营销推广、网站开发设计,对服务成都火锅店设计等多个行业拥有丰富的网站建设及推广经验。成都创新互联公司网站建设公司成立于2013年,提供专业网站制作报价服务,我们深知市场的竞争激烈,认真对待每位客户,为客户提供赏心悦目的作品。 与客户共同发展进步,是我们永远的责任!

* @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,

public void method() {

// dosomeing

}2,

public String metnod() {

String str = "hello";

return str;

}3,

public String metnod(String str) {

str = "hello";

return str;

}函数是由修饰符+返回值类型+方法名(参数)构成,

方法1,2,3的修饰符都是public,1的返回值类型是void,不用返回,没有参数

方法2的返回值是String,无参

方法3返回值是String,有参数

javascrpt代码中的函数有什么作用?

javascrpt中的函数就是包裹在花括号中的代码块,前面使用了关键词 function,当调用该函数时,会执行函数内的代码。 在Javascript中函数实际上就是一个对象,具有引用类型的特征,所以你可以将函数直接传递给变量,这个变量将表示指向函数“对象"的指针。

java中都有哪些函数

java有很多函数,函数就是方法,JDK中有很多包,每个包中有很多类,每个类中都有很多方法。 所以java的函数是很多的。 比如String这个类中,valueOf(),split(),toArrayChar(),等等都是函数。 具体你可以下载jdk的API,里面有所有类和方法的详细说明,不过建议不用背。用的时候直接查API文档就可以了。 如果不懂,可以追问。

Java 的代码,50行,要三个函数,要有循环

随便给你写了一个

package com.wys.util;

import java.util.ArrayList;

import java.util.List;

import java.util.Random;

public class Test {

public static ListInteger smallNumbers,largeNumbers;

public static int sum1 = 0,sum2 = 0;

public static void main(String[] args) {

smallNumbers = new ArrayListInteger();

largeNumbers = new ArrayListInteger();

run();

}

public static void run() {

int i = 0;

for (int j = 0; j 50; j++) {

Random rand = new Random();

i = rand.nextInt(1000);

if (i500) {

small(i);

}else{

large(i);

}

}

System.out.println("随机输出的50个数字中:");

System.out.println("大于500的数(包括500)共有"+largeNumbers.size()+"个");

System.out.print("他们是"+largeNumbers);

System.out.println();

System.out.println("他们的和是"+sum1);

System.out.println("小于500的数共有"+smallNumbers.size()+"个");

System.out.print("他们是"+smallNumbers);

System.out.println();

System.out.println("他们的和是"+sum2);

}

private static void large(int number) {

largeNumbers.add(number);

sum1 += number;

}

private static void small(int number) {

smallNumbers.add(number);

sum2 += number;

}

}


分享标题:java的代码函数 java语言代码
当前链接:http://cdweb.net/article/dooejgi.html