質問するログイン新規登録

質問編集履歴

4

現状のコード記載

2021/03/22 01:59

投稿

Ry_1
Ry_1

スコア1

title CHANGED
File without changes
body CHANGED
@@ -67,4 +67,60 @@
67
67
  }
68
68
 
69
69
  }
70
+ ```
71
+ ### 【追記】現状のコード
72
+ ```JAVA
73
+ package jp.aast.kyoiku;
74
+ import java.io.BufferedReader;
75
+ import java.io.BufferedWriter;
76
+ import java.io.FileInputStream;
77
+ import java.io.FileOutputStream;
78
+ import java.io.InputStreamReader;
79
+ import java.io.OutputStreamWriter;
80
+
81
+
82
+ public class CsvToText {
83
+
84
+ public static void main(final String args[]) {
85
+ try (
86
+ BufferedReader reader = new BufferedReader(new InputStreamReader(
87
+ new FileInputStream(args[0]), "Windows-31J"));
88
+ BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
89
+ new FileOutputStream(args[1]), "Windows-31J"))) {
90
+
91
+ while (reader.ready()) {
92
+ //入力ファイルから1行読み込む。
93
+ final String line = reader.readLine();
94
+ final FileOutputStream os = new FileOutputStream(args[1]);
95
+
96
+ final String enc = "Windows-31J";
97
+
98
+ final String[] items = line.replace("\"", "").split(",");
99
+ //行データを出力ファイルへ書き出す。
100
+ os.write(String.format("%010d", Integer.parseInt(items[0])).getBytes(enc));
101
+ os.write(String.format("%-100s", items[1]).getBytes(enc), 0, 20);
102
+ os.write(String.format("%-100s", items[2]).getBytes(enc), 0, 8);
103
+ os.write(String.format("%-100s", items[3]).getBytes(enc), 0, 8);
104
+ os.write(String.format("%-100s", items[4]).getBytes(enc), 0, 20);
105
+
106
+ os.close();
107
+
108
+ }
109
+
110
+ } catch (Exception e) {
111
+ e.printStackTrace();
112
+
113
+ System.out.println("異常終了しました。");
114
+ System.exit(-1);
115
+ }
116
+
117
+ System.out.println("正常終了しました。");
118
+ System.exit(0);
119
+
120
+ }
121
+
122
+ }
123
+
124
+
125
+
70
126
  ```

3

初心者アイコン表示

2021/03/22 01:59

投稿

Ry_1
Ry_1

スコア1

title CHANGED
File without changes
body CHANGED
@@ -4,7 +4,6 @@
4
4
  "1","コンビニ","100-0000","東京都","Aビル2階"
5
5
  ```
6
6
 
7
-
8
7
  上記のようなCSVファイルをJavaで読み込み、テキストファイルに変換するプログラムを作っています。
9
8
  それぞれ(10バイト,20バイト,8バイト,8バイト,20バイト)の固定長で変換したいと思っています。
10
9
  データ例:

2

誤字

2021/03/21 22:45

投稿

Ry_1
Ry_1

スコア1

title CHANGED
File without changes
body CHANGED
@@ -29,7 +29,7 @@
29
29
  ### 該当のソースコード
30
30
 
31
31
  ```JAVA
32
- public class CsvToText {
32
+ public class Csv {
33
33
 
34
34
  public static void main(String args[]) {
35
35
  try (  //変換前CSVファイル読み込み

1

書式の修正

2021/03/21 06:06

投稿

Ry_1
Ry_1

スコア1

title CHANGED
File without changes
body CHANGED
@@ -17,11 +17,7 @@
17
17
  ```
18
18
  【変換後テキストファイル】
19
19
 
20
- 0000000001
21
- コンビニ□□□□□□
22
- 100-0000
23
- 東京都□
24
- Aビル2階□□□□□␣
20
+ 0000000001コンビニ□□□□□□100-0000東京都□Aビル2階□□□□□␣
25
21
 
26
22
  ```
27
23