Monthly Archives: 1月 2016

Java開発でファイルをコピーする方法まとめ

方法1
public static long custFilecopy(File f1,File f2) throws Exception{
long time=new Date().getTime();
int length=86142;
FileInputStream in=new FileInputStream(f1);
FileOutputStream out=new FileOutputStream(f2);
byte[] buffer=new byte[length];
while(true){
int ins=in.read(buffer);
if(ins==-1){
in.close();
out.flush();
out.close();
return new Date().getTime()-time;
}else
out.write(buffer,0,ins);
}

方法2
public static long pipeCopy(File f1,File f2) throws Exception{
long time=new Date().getTime();
int length=4077162;
FileInputStream in=new FileInputStream(f1);
FileOutputStream out=new FileOutputStream(f2);
FileChannel inC=in.getChannel();
FileChannel outC=out.getChannel();
int i=0;
while(true){
if(inC.position()==inC.size()){
inC.close();
outC.close();
return new Date().getTime()-time;
}
if((inC.size()-inC.position())<31862417)
length=(int)(inC.size()-inC.position());
else
length=31862417;
inC.transferTo(inC.position(),length,outC);
inC.position(inC.position()+length);
i++;
}
}

方法3 
public static long Imgcopy(File f1,File f2) throws Exception{
long time=new Date().getTime();
int length=4097152;
FileInputStream in=new FileInputStream(f1);
RandomAccessFile out=new RandomAccessFile(f2,”rw”);
FileChannel inC=in.getChannel();
MappedByteBuffer outC=null;
MappedByteBuffer inbuffer=null;
byte[] b=new byte[length];
while(true){
if(inC.position()==inC.size()){
inC.close();
outC.force();
out.close();
return new Date().getTime()-time;
}
if((inC.size()-inC.position())<length){
length=(int)(inC.size()-inC.position());
}else{
length=31862417;
}
b=new byte[length];
inbuffer=inC.map(MapMode.READ_ONLY,inC.position(),length);
inbuffer.load();
inbuffer.get(b);
outC=out.getChannel().map(MapMode.READ_WRITE,inC.position(),length);
inC.position(b.length+inC.position());
outC.put(b);
outC.force();
}
}
方法4
public static long ChanFileCopy(File f1,File f2) throws Exception{
long time=new Date().getTime();
int length=3097152;
FileInputStream in=new FileInputStream(f1);
FileOutputStream out=new FileOutputStream(f2);
FileChannel inC=in.getChannel();
FileChannel outC=out.getChannel();
ByteBuffer b=null;
while(true){
if(inC.position()==inC.size()){
inC.close();
outC.close();
return new Date().getTime()-time;
}
if((inC.size()-inC.position())<length){
length=(int)(inC.size()-inC.position());
}else
length=3097152;
b=ByteBuffer.allocateDirect(length);
inC.read(b);
b.flip();
outC.write(b);
outC.force(false);
}
}

JavaScript開発でテキストエリア(textarea)の文字数制限(maxlength)の処理方法

コード下記
var MaxLenCheck = function(){
var stmaxLen = 2048;
this.doKeyPress = function(element, event){
var maxLength = element.getAttribute(“maxLength”);
maxLength = maxLength || stmaxLen;
if (maxLength !== “”) {
if (element.value.length >= maxLength) {
if (window.event) {
window.event.returnValue = null;
}
else {
return false;
}
}
}
this.doOtherThing();
};
/**
* 貼り付け処理
*/
this.doPaste = function(element, event){
var maxLength = element.getAttribute(“maxLength”);
maxLength = maxLength || stmaxLen;
if (maxLength !== “”) {
var cfmt = window.clipboardData.getData(“Text”);
var subStr = cfmt.substr(0, maxLength – element.value.length);
window.clipboardData.setData(“Text”, subStr);
}
this.doOtherThing();
};
this.doOtherThing = function(){
// Client Programer implment this function
};
}

Jquery開発で全角を半角に、半角を全角に変換するサンプルコード

コード下記:
(function($){
$.extend({
//全角を半角に変換
quanToBan : function(str){
var zenkaku=””;
var charCode;
for (var i = 0; i < str.length; i++) {
charCode = str.charCodeAt(i);
//スペース変換
if (charCode==12288) {
zenkaku+= String.fromCharCode(charCode-12256);
continue;
}

if (charCode > 65280 && charCode <65375)
zenkaku+= String.fromCharCode(charCode – 65248);
else zenkaku+= String.fromCharCode(charCode);
}

return zenkaku;
},

//半角を全角に変換
bantoQuan: function(str){
var hankaku = “”;
var charCode;
for(var i=0;i<str.length;i++) {
charCode = str.charCodeAt(i);
if(charCode == 32) {
hankaku= hankaku+ String.fromCharCode(12288);
}

if(charCode > 33 && charCode < 127) {
hankaku=hankaku+String.fromCharCode(charCode + 65248);
} else {
hankaku=hankaku+String.fromCharCode(charCode);
}
}

return hankaku;
}
});
}(jQuery))

Javaでフォルダを再帰するサンプルコード

コードメモ:
import java.io.File;
import java.util.ArrayList;
public class FileSysCheck {
private static ArrayList filelist = new ArrayList();
public static void main(String[] args) {
long cr= System.currentTimeMillis();
reFileList(“E:\\java_startnews24”);
System.out.println(System.currentTimeMillis() – cr);
}
public static void reFileList(String strPath) {
File dir = new File(strPath);
File[] files = dir.listFiles();

if (files == null)
return;
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
reFileList(files[i].getAbsolutePath());
} else {
String strFile = files[i].getAbsolutePath().toLowerCase();
System.out.println(“—“+strFile);
filelist.add(files[i].getAbsolutePath());
}
}
}
}

javaでディレクトリを作成する方法

javaコード

static public void createFolder(String filepath) {
if (filepath.lastIndexOf(File.separator)>-1) {
String folderpath = filepath.substring(0,filepath.lastIndexOf(File.separator));
File dir = new File(folderpath);
if (! dir.exists()) {
dir.mkdirs();
}
} else if (filepath.lastIndexOf(“/”)>-1) {
String folderpath = filepath.substring(0,filepath.lastIndexOf(“/”));
File dir = new File(folderpath);
if (! dir.exists()) {
dir.mkdirs();
}
} else if (filepath.lastIndexOf(“\\”)>-1) {
String folderpath = filepath.substring(0,filepath.lastIndexOf(“\\”));
File dir = new File(folderpath);
if (! dir.exists()) {
dir.mkdirs();
}
}
}

JavaScriptでhtml特殊符号を変換するサンプルコード

コード下記
var regSpeChars = [ “^”,”$”,”(“,”)”,”[“,”]”,”{“,”}”,”.”,”?”,”+”,”*”,”|”,”\\”];
var speCharsMap = {“&”: “&amp;”,
“#”:”&#35″,
“>” : “&gt;”,
“<” : “&lt;”,
“‘”:”&lsquo”,
“’”:”&rsquo”,
“,”:”&sbquo”,
“‘”:”&#39″,
“\\”:”&#92;”,
“\””:”&ldquo;”,
“!”:”&#33″,
“%”:”&#37″,
“~”:”&#126″,
“@”:”&#64″,
“=”:”&#61″,
“(“:”&#40”,
“)”:”&#41″,
“{“:”&#123”,
“}”:”&#125″,
“[“:”&#91;”,
“]”:”&#93;”,
“$”:”&#36″,
“?”:”&#63″,
“*”:”&#42;”,
“|”:”&#124;”,
“^”:”&#94;”,
“.”:”&#46;”,
“+”:”&#43;”,
“/”:”&#47;”,
” “:”&nbsp;”
};
function changeElementSpecialChars(elementId){
var element = document.getElementById(elementId);
var value = changeSpecialChars(element.value);
element.value = value;
}
function changeSpecialChars(value) {
if(value==null || value == “”)
return value;
for(var key in speCharsMap){
var regex = keyToRegex(key);
value = value.replace(regex,speCharsMap[key]);
}
return value;
}

function keyToRegex(key){
if(isRegexSpecialChar(key)){
key=”\\”+key;
}
var regex = new RegExp(key,”g”);
return regex;
}

function isRegexSpecialChar(character){
var length = regSpeChars.length;
var ch;
for(var index = 0; index < length; index++ ) {
ch = regSpeChars[index];
if(character == ch) return true;
}
return false;
}

3 / 3123