不知道是否理解对了你的意思,大概写了一下:
公司主营业务:成都网站制作、网站建设、外贸网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联推出武平免费做网站回馈大家。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.StringWriter;
public class FileReadAndWrite {
private static final int DEFAULT_BUFFER_SIZE = 1024;
public static void main(String[] args) {
File file = new File("E:/workspace/FileIOTest/src/a.txt");
String str = file2String(file, "UTF-8");
str = str.replace('d', 'f');
string2File(str,"E:/workspace/FileIOTest/src/b.txt");
System.out.println("处理完毕");
}
/**
* 文本文件转换为指定编码的字符串
*
* @param file
* 文本文件
* @param encoding
* 编码类型
* @return 转换后的字符串
* @throws IOException
*/
public static String file2String(File file, String encoding) {
InputStreamReader reader = null;
StringWriter writer = new StringWriter();
try {
if (encoding == null || "".equals(encoding.trim())) {
reader = new InputStreamReader(new FileInputStream(file),
encoding);
} else {
reader = new InputStreamReader(new FileInputStream(file));
}
// 将输入流写入输出流
char[] buffer = new char[DEFAULT_BUFFER_SIZE];
int n = 0;
while (-1 != (n = reader.read(buffer))) {
writer.write(buffer, 0, n);
}
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (reader != null)
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 返回转换结果
if (writer != null)
return writer.toString();
else
return null;
}
/**
* 将字符串写入指定文件(当指定的父路径中文件夹不存在时,会最大限度去创建,以保证保存成功!)
*
* @param res
* 原字符串
* @param filePath
* 文件路径
* @return 成功标记
*/
public static boolean string2File(String res, String filePath) {
boolean flag = true;
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
try {
File distFile = new File(filePath);
if (!distFile.getParentFile().exists())
distFile.getParentFile().mkdirs();
bufferedReader = new BufferedReader(new StringReader(res));
bufferedWriter = new BufferedWriter(new FileWriter(distFile));
char buf[] = new char[1024]; // 字符缓冲区
int len;
while ((len = bufferedReader.read(buf)) != -1) {
bufferedWriter.write(buf, 0, len);
}
bufferedWriter.flush();
bufferedReader.close();
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
flag = false;
return flag;
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return flag;
}
}
最简单的java代码肯定就是这个了,如下:
public class MyFirstApp
{
public static void main(String[] args)
{
System.out.print("Hello world");
}
}
“hello world”就是应该是所有学java的新手看的第一个代码了。如果是零基础的新手朋友们可以来我们的java实验班试听,有免费的试听课程帮助学习java必备基础知识,有助教老师为零基础的人提供个人学习方案,学习完成后有考评团进行专业测试,帮助测评学员是否适合继续学习java,15天内免费帮助来报名体验实验班的新手快速入门java,更好的学习java!
public class Message {
public static void main(String[] args){
String name;
int age;
System.out.println("请输入姓名,回车结束:"); //提示输入
Scanner sc = new Scanner(System.in);
name = sc.nextLine(); //为变量赋值
System.out.println("请输入年龄,回车结束:");
age = sc.nextInt();
System.out.println("姓名:"+name+"\n年龄:"+age); //打印姓名及年龄
}
}
//不知道这样行么?
数据少的话 模糊查找,多的话,用lucene
建立索引,从索引里面搜索。
索引搜索是这么做的:
package method;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.LockObtainFailedException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import pojo.user;
import Interface.dao;
public class CreateAndUse extends HibernateDaoSupport implements dao{
private static String INDEX_PATH = "D:/use/";//索引路径
public void create(){
Listuser list = getHibernateTemplate().find("from user");
try{
IndexWriter writer = new IndexWriter(CreateAndUse.INDEX_PATH, new StandardAnalyzer(),true);
for(int i=0;ilist.size();i++){
Document doc = new Document();
Field f1 = new Field("id",String.valueOf(list.get(i).getId()),Field.Store.YES,Field.Index.UN_TOKENIZED);
Field f2 = new Field("username",list.get(i).getUsername(),Field.Store.YES,Field.Index.UN_TOKENIZED);
Field f3 = new Field("password",list.get(i).getPassword(),Field.Store.YES,Field.Index.TOKENIZED);
doc.add(f1);
doc.add(f2);
doc.add(f3);
writer.addDocument(doc);
}
writer.close();
}catch(Exception e){
e.printStackTrace();
}
}
public Listuser search(String str,String type) throws CorruptIndexException, IOException, ParseException{
Listuser list = new ArrayList();
user u = null;
IndexSearcher index = new IndexSearcher(INDEX_PATH);
QueryParser q = new QueryParser(type,new StandardAnalyzer());
q.setDefaultOperator(QueryParser.AND_OPERATOR);
Query query = q.parse(str);
Hits hits = index.search(query);
// System.out.println("hits的长度为:"+hits.length());
for(int i = 0;i hits.length();i++){
u = new user();
u.setCount(hits.length());
u.setId(Integer.parseInt(hits.doc(i).get("id")));
u.setUsername(hits.doc(i).get("username"));
u.setPassword(hits.doc(i).get("password"));
list.add(u);
}
return list;
}
}
lucene是个java的搜索库,你要自己下载配置一下这个库。