2019新版《龙果学院Elasticsearch顶尖高手系列(高手进阶篇教程)》
作者:互联网
#sql语句
show create table 表名; #查询表的信息(主要看字符集)
/*例:Table Create Table
Dog CREATE TABLE `dog` (
`dog_id` int(11) NOT NULL AUTO_INCREMENT,
......
PRIMARY KEY (`dog_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8*/
#修改完数据库字符集,需要重启mysql数据库
ALTER DATABASE 数据库名 CHARACTER SET utf8;
#修改表字符集
ALTER TABLE 表名 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
public class DateUtil0 {
private static final String MESSAGE_FORMAT = "MM-dd HH:mm:ss.ms";
private static final SimpleDateFormat format=new SimpleDateFormat(MESSAGE_FORMAT, Locale.getDefault());
public static final DateFormat getDateFormat() {
return format;
}
public Date parse(String str) {
try {
synchronized(format){
return format.parse(str);
}
} catch (ParseException e) {
}
return null;
}
}
public class DateUtil3 {
private static final String MESSAGE_FORMAT = "MM-dd HH:mm:ss.ms";
private static final ThreadLocal messageFormat = new ThreadLocal(){
protected synchronized Object initialValue() {
return null;
}
};
private static final DateFormat getDateFormat() {
DateFormat df = (DateFormat) messageFormat.get();
if (df == null) {
df = new SimpleDateFormat(MESSAGE_FORMAT, Locale.getDefault());
messageFormat.set(df);
}
return df;
}
}
public class DemoInterceptor implements HandlerInterceptor {
//在进入控制器之前执行
//如果返回值为 false,阻止进入控制器
//控制代码
@Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception
{
System.out.println("arg2:"+arg2);
System.out.println("preHandle");
return true;
}
//控制器执行完成,进入到 jsp 之前执行.
//日志记录.
//敏感词语过滤
@Override
public void postHandle(HttpServletRequest arg0,HttpServletResponse arg1, Object arg2, ModelAndView arg3)throws Exception {
System.out.println("往"+arg3.getViewName()+"跳转");
System.out.println("model 的值"+arg3.getModel().get("model"));
String word =arg3.getModel().get("model").toString();
String newWord = word.replace("祖国", "**");
arg3.getModel().put("model", newWord);
// arg3.getModel().put("model", "修改后的内容");
System.out.println("postHandle");
}
//jsp 执行完成后执行
//记录执行过程中出现的异常.
//可以把异常记录到日志中
@Override
public void afterCompletion(HttpServletRequest arg0,HttpServletResponse arg1, Object arg2, Exception arg3)throws Exception {
System.out.println("afterCompletion"+arg3.getMessage());
}
}
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
signals:
void sendStr();
private:
Ui::Widget *ui;
QPushButton* pushButton;
SubThread subThread;
public slots:
void on_click_button();
};
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
qDebug() << "widget thread id:" << QThread::currentThreadId();
pushButton = new QPushButton(this);
pushButton->setText(QString("button"));
connect(pushButton, &QPushButton::clicked, this, &Widget::on_click_button);
connect(this, &Widget::sendStr, &subThread, &SubThread::showStr, Qt::AutoConnection);
subThread.start();
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_click_button()
{
qDebug() << "send signal thread id:" << QThread::currentThreadId();
emit sendStr();
}//字符输入缓冲流
private static void charReader() {
//目标文件
File file = new File("F:\\javatest\\lemon1.txt");
try {
//字符流
Reader reader = new FileReader(file);
//为字符流提供缓冲,已达到高效读取的目的
BufferedReader bufr = new BufferedReader(reader);
char[] chars = new char[1024];
int len = -1;
while((len = bufr.read(chars)) != -1) {
System.out.println(new String(chars,0,len));
}
bufr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}package com.lemon;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
/**
* 缓存的目的:
* 解决在写入文件操作时,频繁的操作文件所带来的性能降低问题
* BufferedOutputStream内部默认的缓存大小是8kb,每次写入时存储到缓存中的byte数组中,当数组存满时,会把数组中的数据写入文件
* 并且缓存下标归零
*
* 字符流:
* 1、加入字符缓存流,增强读取功能(readLine)
* 2、更高效的读取数据
* FileReader:内部使用InputStreamReader,解码过程,byte->char,默认缓存大小为8k
* BufferReader:默认缓存大小为8k,但可以手动指定缓存大小,把数据读取到缓存中,减少每次转换过程,效率更高
* BufferedWriter:同上
* @author lemonSun
*
* 2019年5月4日下午8:12:53
*/
public class BufferStreamDemo {
public static void main(String[] args) {
// byteWriter();
// byteReader();
// byteReader1();
// charReader();
charWriter();
}
//字符输出缓存流
private static void charWriter() {
//目标文件
File file = new File("F:\\javatest\\lemon1.txt");
try {
//字符流
Writer writer = new FileWriter(file,true);//追加
//为字符流提供缓冲,已达到高效读取的目的
BufferedWriter bufr = new BufferedWriter(writer);
bufr.write("这里是字符缓冲流\r\n");
bufr.flush();
bufr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//字符输入缓存流
private static void charReader() {
//目标文件
File file = new File("F:\\javatest\\lemon1.txt");
try {
//字符流
Reader reader = new FileReader(file);
//为字符流提供缓冲,已达到高效读取的目的
BufferedReader bufr = new BufferedReader(reader);
char[] chars = new char[1024];
int len = -1;
while((len = bufr.read(chars)) != -1) {
System.out.println(new String(chars,0,len));
}
bufr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//缓存流输入 不用关闭 try自动关闭 必须实现Closeable接口
private static void byteReader1(){
//目标文件
File file = new File("F:\\javatest\\lemon1.txt");
//buf作用域在try大括号里面多条语句try(;),;隔开
try(BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file))) {
byte[] bytes = new byte[1024];
int len = -1;
while((len = buf.read(bytes)) != -1) {
System.out.println(new String(bytes,0,len));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//缓存流输入
private static void byteReader(){
//目标文件
File file = new File("F:\\javatest\\lemon1.txt");
try {
//字节输出流
InputStream in = new FileInputStream(file);
//字节缓冲流
BufferedInputStream buf = new BufferedInputStream(in);
byte[] bytes = new byte[1024];
int len = -1;
while((len = buf.read(bytes)) != -1) {
System.out.println(new String(bytes,0,len));
}
buf.close();//自动关闭 in.close
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//缓存流输出
private static void byteWriter(){
//目标文件
File file = new File("F:\\javatest\\lemon1.txt");
try {
//字节输出流
OutputStream out = new FileOutputStream(file,true);
//缓冲流
BufferedOutputStream buf = new BufferedOutputStream(out);
//内容
String info = "这里是缓冲流\r\n";
//写入
buf.write(info.getBytes());
buf.close(); //jdk1.7以后自动关闭 out
// out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class Mytest1 {
public static void main(String[] args) {
int num=100;
//转换为二进制
String string = Integer.toBinaryString(num);
//转换为八进制
String string1 = Integer.toOctalString(num);
转换为十六进制
String string2 = Integer.toHexString(num);
//将返回的二进制字符串转换成其对应的数字
System.out.println(Integer.parseInt(string)+45);
System.out.println(string1);
System.out.println(string2);
}
}
package org.westos.demo2;
public class Mytest6 {
public static void main(String[] args) {
Integer i1 = new Integer(127);
Integer i2 = new Integer(127);
System.out.println(i1 == i2);//false
System.out.println(i1.equals(i2));//true
System.out.println("-----------");
Integer i3 = new Integer(128);
Integer i4 = new Integer(128);
System.out.println(i3 == i4);//false
System.out.println(i3.equals(i4));//true
System.out.println("-----------");
Integer i5 = 128;
Integer i6 = 128;
System.out.println(i5 == i6);//false 因为 超过了一个字节的范围 会new 一个Integer对象
System.out.println(i5.equals(i6));//true
System.out.println("-----------");
Integer i7 = 127;
Integer i8 = 127;
System.out.println(i7 == i8);//true 没有超过一个字节的范围 因为在方法区中存在一个 字节常量池 范围-128---127
System.out.println(i7.equals(i8));//true
}
}
public class FileWriteDemo {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("d:\\a.txt");
//调用输出流对象写数据的方法
//写一个字符串
fw.write("I love you 我爱你");
//数据没有直接写到文件,而是写到了缓冲区
//刷新
fw.flush();
//释放资源
//通知系统释放和该文件相关的资源
fw.close();
}
}
package test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
public class testJson {
/**
* @param args
*/
public static void main(String[] args)
{
String path = "C:\\Users\\i042416\\Desktop\\1.txt";
File file = new File(path);
StringBuffer buffer = new StringBuffer();
InputStreamReader read;
try
{
read = new InputStreamReader( new FileInputStream(file));
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine() ) != null)
{
buffer.append(lineTxt);
}
read.close();
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println("content: " + buffer.toString());
JSON json = JSONSerializer.toJSON(buffer.toString());
JSONObject jsonObject = JSONObject.fromObject(json);
JSONArray array = jsonObject.getJSONArray("statuses");
int size = array.size();
System.out.println("total post number: " + size);
for( int i = 0; i < size; i++)
{
JSONObject post = array.getJSONObject(i);
System.out.println("****************************************************");
System.out.println("Post Index: " + i);
String id = post.getString("idstr");
System.out.println("Post ID: " + id);
System.out.println("Post content: " + post.getString("text"));
System.out.println("Created at: " + post.getString("created_at"));
JSONObject user = array.getJSONObject(i).getJSONObject("user");
System.out.println("user ID: " + user.getString("idstr"));
System.out.println("name: " + user.getString("name"));
}
}
}
class Myclass(object):
def __init__(self):#在类对象的内部(方法中)绑定实例属性
self.ial = 1S
def do_sth1(self):#在类对象的内部(方法中)访问实例属性
print(self.ia1)
def do_sth2(self):
print(self.ia2)
def do_another(self):#在类对象的内部(方法中)绑定实例属性
self.ia2 = 56
def do_sth3(self):
print(self.ia3)
class MyClass (ob ject):
#在类对象内部 (方法外)绑定类属性
cal = 66
def do_ sth(self) :
#在类对象内部(方法中)访问类属性
print (MyClass. cal)
def do_ another (self) :
#在类对象内部(方法中)访问类属性
print (MyClass. ca2)
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
//PrintWriter out = response.getWriter();
String name = request.getParameter("name");
String pass = request.getParameter("password");
System.out.println("读取表格成功");
JavaBSession peo = new JavaBSession();
peo.setName(name);
peo.setPassword(pass);
JavaBSession peo1 = new JavaBSession();
Thelogin loginThelogin = new Thelogin();
peo1 = loginThelogin.loginjc(peo);
String s = URLEncoder.encode(name, "GBK");
ServletContext context = this.getServletContext();
context.setAttribute("username", s);
if(peo1!=null){
System.out.println("登陆成功");
String use = request.getSession().getId()+s;
System.out.println("登陆session:"+use);
Cookie cookie[] = request.getCookies();
String time = null;
for(int i=0;i<cookie.length&&cookie!=null;i++){
if(use.equals(cookie[i].getName()))
time = cookie[i].getValue();
}
if(time!=null){
System.out.println(name+"上次登陆时间:"+time);
}
else {
System.out.println("第一次登陆");
}
String timesString = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
Cookie cookie2 = new Cookie(use, timesString);
System.out.println("本次登录时间"+timesString);
response.addCookie(cookie2);
System.out.println("增加cookie成功");
request.getRequestDispatcher("index.jsp").forward(request, response);
}
else{
System.out.println("登陆失败");
request.getRequestDispatcher("MyLog.jsp").forward(request, response);
}
}
标签:顶尖高手,String,System,进阶篇,Elasticsearch,import,println,new,out 来源: https://blog.csdn.net/weixin_44995021/article/details/90380890