java.io.FileWriterで文字ファイルを書き込むサンプルコード

Javaコード
String str=”hello world!”;
FileWriter writer;
try {
writer = new FileWriter(“D:/demo.txt”);
writer.write(str);
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}

java.io.OutputStream.FileOutputStreamのサンプルコード

Javaコード
File txt=new File(“D:/demotxt”);
if(!txt.exists()){
txt.createNewFile();
}
byte bytes[]=new byte[512];
bytes=str.getBytes();
int b=bytes.length;
FileOutputStream fos=new FileOutputStream(txt);
fos.write(bytes,0,b);
fos.write(bytes);
fos.close();

「jQuery」id値/タグ名/クラス名で特定の要素を取り出す方法

1.id値で要素を取り出す
var div = $(‘#abc’);
2.タグで要素を取り出す
var ps = $(‘p’);
ps.length;
3.クラス名でタグで要素を取り出す
var a = $(‘.red’);

「Android」Viewの内容をBitmapの保存方法

Javaコード:
public Bitmap createViewBitmap(View v) {
Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
v.draw(canvas);
return bitmap;
}

Windows10にWifiのプロファイルを削除するメモ

1.Windows10のスタートメニューの「設定」を選択します

2.「ネットワークとインターネット」を選択します

3.Wifiを選択します

4.既知のネットワークの管理を選択します

5.「BUFFALO-XXX-A」を削除

CentOS7.4でIPコマンドをメモする

1.環境の確認
# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)

2.IPコマンド

# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 9c:a3:ba:01:e1:a5 brd ff:ff:ff:ff:ff:ff
inet xxx.xxx.xxx.55/23 brd xxx.xxx.xxx.255 scope global eth0
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 9c:a3:ba:03:68:45 brd ff:ff:ff:ff:ff:ff
4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 9c:a3:ba:04:ee:e5 brd ff:ff:ff:ff:ff:ff

 

3.特定デバイスの状態確認
ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet xxx.xxx.xxx.55 netmask 255.255.254.0 broadcast xxx.xxx.xxx.255
ether 9c:a3:ba:01:e1:a5 txqueuelen 1000 (Ethernet)
RX packets 14156471 bytes 920928982 (878.2 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 3618059 bytes 4088711994 (3.8 GiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

4.デバイスごとのパケット処理数
# netstat -i
Kernel Interface table
Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0 1500 14159495 0 0 0 3618824 0 0 0 BMRU
eth1 1500 0 0 0 0 0 0 0 0 BMRU
eth2 1500 0 0 0 0 0 0 0 0 BMRU
lo 65536 1583651 0 0 0 1583651 0 0 0 LRU

CentOS7にlshwでディスクの型番を取得する

# yum install lshw
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* remi-safe: mirror.bebout.net
Resolving Dependencies
–> Running transaction check
—> Package lshw.x86_64 0:B.02.17-12.el7 will be installed
–> Processing Dependency: hwdata for package: lshw-B.02.17-12.el7.x86_64
–> Running transaction check
—> Package hwdata.x86_64 0:0.252-8.4.el7 will be installed
–> Finished Dependency Resolution

Dependencies Resolved

================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
lshw x86_64 B.02.17-12.el7 base 286 k
Installing for dependencies:
hwdata x86_64 0.252-8.4.el7 base 2.1 M

Transaction Summary
================================================================================
Install 1 Package (+1 Dependent package)

Total download size: 2.4 M
Installed size: 13 M
Is this ok [y/d/N]: y
Downloading packages:
(1/2): lshw-B.02.17-12.el7.x86_64.rpm | 286 kB 00:00
(2/2): hwdata-0.252-8.4.el7.x86_64.rpm | 2.1 MB 00:00
——————————————————————————–
Total 5.3 MB/s | 2.4 MB 00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : hwdata-0.252-8.4.el7.x86_64 1/2
Installing : lshw-B.02.17-12.el7.x86_64 2/2
Verifying : lshw-B.02.17-12.el7.x86_64 1/2
Verifying : hwdata-0.252-8.4.el7.x86_64 2/2

Installed:
lshw.x86_64 0:B.02.17-12.el7

Dependency Installed:
hwdata.x86_64 0:0.252-8.4.el7

Complete!

# sudo lshw -c disk
*-cdrom
description: SCSI CD-ROM
physical id: 0.0.0
bus info: scsi@1:0.0.0
logical name: /dev/cdrom
logical name: /dev/sr0
capabilities: audio
configuration: status=nodisc
*-virtio3
description: Virtual I/O device
physical id: 0
bus info: virtio@3
logical name: /dev/vda
size: 200GiB (214GB)
capabilities: gpt-1.00 partitioned partitioned:gpt
configuration: driver=virtio_blk guid=24e50a8f-eb81-4573-9f30-1084d97ebbc2 logicalsectorsize=512 sectorsize=512

「Spring MVC」LdapTemplateを初期化するサンプルコード

サンプルコード:
private static final LdapTemplate template;
static {
LdapContextSource cs = new LdapContextSource();
cs.setCacheEnvironmentProperties(false);
cs.setUrl(“ldap://172.17.2.200:10389”);
cs.setBase(“dc=mzone,dc=cc”);
cs.setAuthenticationSource(new AuthenticationSource() {
@Override
public String getCredentials() {
return “mzonecc”;
}

@Override
public String getPrincipal() {
return “uid=admin,ou=system”;
}
});
template = new LdapTemplate(cs);
}

「Java」正規表現でメールアドレスを判定するコード

サンプルコード
/**
* メールアドレスのチェック
*
* @param email
* @return
*/
public static boolean checkEmail(String email) {
boolean flag = false;
try {
String check = “^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$”;
Pattern regex = Pattern.compile(check);
Matcher matcher = regex.matcher(email);
flag = matcher.matches();
} catch (Exception e) {
flag = false;
}
return flag;
}

[Spring MVC]ファイルをダウンロードするコード

Javaコード
/**
* ダウンロード
* @param request
* @param response
* @param storeName
* @param contentType
* @param realName
* @throws Exception
*/
public static void download(HttpServletRequest request,
HttpServletResponse response, String storeName, String contentType,
String realName) throws Exception {
response.setContentType(“text/html;charset=UTF-8”);
request.setCharacterEncoding(“UTF-8”);
BufferedInputStream bis = null;
BufferedOutputStream bos = null;

String ctxPath = request.getSession().getServletContext()
.getRealPath(“/”)
+ FileOperateUtil.UPLOADDIR;
String downLoadPath = ctxPath + storeName;

long fileLength = new File(downLoadPath).length();

response.setContentType(contentType);
response.setHeader(“Content-disposition”, “attachment; filename=”
+ new String(realName.getBytes(“utf-8”), “ISO8859-1”));
response.setHeader(“Content-Length”, String.valueOf(fileLength));

bis = new BufferedInputStream(new FileInputStream(downLoadPath));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
bis.close();
bos.close();
}
}