「java原子编码」java中的原子类
今天给各位分享java原子编码的知识,其中也会对java中的原子类进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
Java中如何实现原子操作
Java中的原子操作包括:
1)除long和double之外的基本类型的赋值操作
2)所有引用reference的赋值操作
3)java.concurrent.Atomic.* 包中所有类的一切操作
count++不是原子操作,是3个原子操作组合
1.读取主存中的count值,赋值给一个局部成员变量tmp
2.tmp+1
3.将tmp赋值给count
可能会出现线程1运行到第2步的时候,tmp值为1;这时CPU调度切换到线程2执行完毕,count值为1;切换到线程1,继续执行第3步,count被赋值为1------------结果就是两个线程执行完毕,count的值只加了1;
还有一点要注意,如果使用AtomicInteger.set(AtomicInteger.get() + 1),会和上述情况一样有并发问题,要使用AtomicInteger.getAndIncrement()才可以避免并发问题
Java 源代码的编码问题
//我写了一个程序,你把文字复制到文本框中点转码按钮,就可以了
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class Class1 extends JFrame {
private final class AT implements ActionListener {
public void actionPerformed(ActionEvent e) {
jta.setText(fixString(jta.getText()));
}
}
public static void main(String[] args) {
JFrame jf = new Class1();
}
JTextArea jta = new JTextArea();
public Class1() {
setSize(400, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JButton jb = new JButton("转码");
jb.addActionListener(new AT());
add(jta);
add("North", jb);
setVisible(true);
}
String fixString(String s) {
while (true) {
int index = s.indexOf("\\u");
if (index != -1) {
String s1 = s.substring(index, index + 6);
if (s1.matches("\\\\u[0-9A-F]{4}")) {
char c = (char) Integer.parseInt(s1.replace("\\u", ""), 16);
s = s.substring(0, index) + c + s.substring(index + 6);
}
} else {
break;
}
}
return s;
}
}
如何获取java源文件编码格式
java中主要使用charset这个类来判断文件的编码格式,代码如下:
package com.ghj.packageoftool;
import info.monitorenter.cpdetector.io.ASCIIDetector;
import info.monitorenter.cpdetector.io.ByteOrderMarkDetector;
import info.monitorenter.cpdetector.io.CodepageDetectorProxy;
import info.monitorenter.cpdetector.io.JChardetFacade;
import info.monitorenter.cpdetector.io.ParsingDetector;
import info.monitorenter.cpdetector.io.UnicodeDetector;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.Charset;
/**
* 文件工具类
*
* @author 高焕杰
*/
public class FileTool {
/**
* 获取本地文件的编码格式
*
* @param file 要判断的文件编码格式
*
* @author 高焕杰
*/
public static String getLocalFileEncode(File localFile) {
/*
* cpDetector是探测器,它把探测任务交给具体的探测实现类的实例完成。
* cpDetector内置了一些常用的探测实现类,这些探测实现类的实例可以通过add方法 加进来,如ParsingDetector、ByteOrderMarkDetector、JChardetFacade、ASCIIDetector、UnicodeDetector。
* cpDetector按照“谁最先返回非空的探测结果,就以该结果为准”的原则返回探测到的字符集编码。cpDetector是基于统计学原理的,不保证完全正确。
*/
CodepageDetectorProxy codepageDetector = CodepageDetectorProxy.getInstance();
codepageDetector.add(new ParsingDetector(false));//ParsingDetector可用于检查HTML、XML等文件或字符流的编码,构造方法中的参数用于指示是否显示探测过程的详细信息,为false不显示。
codepageDetector.add(JChardetFacade.getInstance());//JChardetFacade封装了由Mozilla组织提供的JChardet,它可以完成大多数文件的编码 测定。所以,一般有了这个探测器就可满足大多数项目的要求,如果你还不放心,可以再多加几个探测器,比如下面的ASCIIDetector、UnicodeDetector等。
codepageDetector.add(new ByteOrderMarkDetector());
codepageDetector.add(ASCIIDetector.getInstance());//ASCIIDetector用于ASCII编码测定
codepageDetector.add(UnicodeDetector.getInstance());//UnicodeDetector用于Unicode家族编码的测定
Charset charset = null;
try {
charset = codepageDetector.detectCodepage(localFile.toURI().toURL());
if (charset != null){
return charset.name();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 获得远程URL文件的编码格式
*
* @param url 远程文件的URL路径
*
* @author 高焕杰
*/
public static String getURLFileEncode(URL url) {
/*
* cpDetector是探测器,它把探测任务交给具体的探测实现类的实例完成。
* cpDetector内置了一些常用的探测实现类,这些探测实现类的实例可以通过add方法 加进来,如ParsingDetector、ByteOrderMarkDetector、JChardetFacade、ASCIIDetector、UnicodeDetector。
* cpDetector按照“谁最先返回非空的探测结果,就以该结果为准”的原则返回探测到的字符集编码。cpDetector是基于统计学原理的,不保证完全正确。
*/
CodepageDetectorProxy codepageDetector = CodepageDetectorProxy.getInstance();
codepageDetector.add(new ParsingDetector(false));//ParsingDetector可用于检查HTML、XML等文件或字符流的编码,构造方法中的参数用于指示是否显示探测过程的详细信息,为false不显示。
codepageDetector.add(JChardetFacade.getInstance());//JChardetFacade封装了由Mozilla组织提供的JChardet,它可以完成大多数文件的编码 测定。所以,一般有了这个探测器就可满足大多数项目的要求,如果你还不放心,可以再多加几个探测器,比如下面的ASCIIDetector、UnicodeDetector等。
codepageDetector.add(ASCIIDetector.getInstance());//ASCIIDetector用于ASCII编码测定
codepageDetector.add(UnicodeDetector.getInstance());//UnicodeDetector用于Unicode家族编码的测定
Charset charset = null;
try {
charset = codepageDetector.detectCodepage(url);
if (charset != null){
return charset.name();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 获得文件流的编码格式
*
* @param inputStream 文件流
*
* @author 高焕杰
*/
public static String getInputStreamEncode(InputStream inputStream) {
/*
* cpDetector是探测器,它把探测任务交给具体的探测实现类的实例完成。
* cpDetector内置了一些常用的探测实现类,这些探测实现类的实例可以通过add方法 加进来,如ParsingDetector、ByteOrderMarkDetector、JChardetFacade、ASCIIDetector、UnicodeDetector。
* cpDetector按照“谁最先返回非空的探测结果,就以该结果为准”的原则返回探测到的字符集编码。cpDetector是基于统计学原理的,不保证完全正确。
*/
CodepageDetectorProxy codepageDetector = CodepageDetectorProxy.getInstance();
codepageDetector.add(new ParsingDetector(false));//ParsingDetector可用于检查HTML、XML等文件或字符流的编码,构造方法中的参数用于指示是否显示探测过程的详细信息,为false不显示。
codepageDetector.add(JChardetFacade.getInstance());//JChardetFacade封装了由Mozilla组织提供的JChardet,它可以完成大多数文件的编码 测定。所以,一般有了这个探测器就可满足大多数项目的要求,如果你还不放心,可以再多加几个探测器,比如下面的ASCIIDetector、UnicodeDetector等。
codepageDetector.add(ASCIIDetector.getInstance());//ASCIIDetector用于ASCII编码测定
codepageDetector.add(UnicodeDetector.getInstance());//UnicodeDetector用于Unicode家族编码的测定
Charset charset = null;
try {
charset = codepageDetector.detectCodepage(inputStream, 0);
if (charset != null){
return charset.name();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 获得字符串的编码格式
*
* @param stringValue 要判断的文件编码格式字符串
*
* @author 高焕杰
*/
public static String getStringEncode(String stringValue) {
/*
* cpDetector是探测器,它把探测任务交给具体的探测实现类的实例完成。
* cpDetector内置了一些常用的探测实现类,这些探测实现类的实例可以通过add方法 加进来,如ParsingDetector、ByteOrderMarkDetector、JChardetFacade、ASCIIDetector、UnicodeDetector。
* cpDetector按照“谁最先返回非空的探测结果,就以该结果为准”的原则返回探测到的字符集编码。cpDetector是基于统计学原理的,不保证完全正确。
*/
CodepageDetectorProxy codepageDetector = CodepageDetectorProxy.getInstance();
codepageDetector.add(new ParsingDetector(false));//ParsingDetector可用于检查HTML、XML等文件或字符流的编码,构造方法中的参数用于指示是否显示探测过程的详细信息,为false不显示。
codepageDetector.add(JChardetFacade.getInstance());//JChardetFacade封装了由Mozilla组织提供的JChardet,它可以完成大多数文件的编码 测定。所以,一般有了这个探测器就可满足大多数项目的要求,如果你还不放心,可以再多加几个探测器,比如下面的ASCIIDetector、UnicodeDetector等。
codepageDetector.add(ASCIIDetector.getInstance());//ASCIIDetector用于ASCII编码测定
codepageDetector.add(UnicodeDetector.getInstance());//UnicodeDetector用于Unicode家族编码的测定
Charset charset = null;
try {
InputStream inputStream = new ByteArrayInputStream(stringValue.getBytes());
charset = codepageDetector.detectCodepage(inputStream, 3);
if (charset != null){
return charset.name();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
关于java原子编码和java中的原子类的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。