我正在尝试使用try和catch创建文本格式的逗号分隔文件。为此,我正在使用库PrintWriter。但是,我还不能互相打印所有行。我创建了一个列表来存储每次迭代中创建的每一行,然后尝试在列表上使用for来将每个元素打印为文件中的一行:
private void initializeDraggablePanel(Fragment fragmentView, Fragment bottomFragment) {
draggablePanel.removeAllViews();
draggablePanel.setFragmentManager(getSupportFragmentManager());
draggablePanel.setTopFragment((Fragment) fragmentView);
draggablePanel.setBottomFragment(bottomFragment);
draggablePanel.setVisibility(View.VISIBLE);
draggablePanel.setClickToMaximizeEnabled(true);
draggablePanel.setDraggableListener(new DraggableListener() {
@Override
public void onClosedToLeft() {
draggablePanel.removeAllViews();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
@Override
public void onMinimized() {
isFullScFromMini = true;
pipHandler.removeCallbacks(runnapipa);
pipHandler.postDelayed(runnapipa, PIP_DELAY);
draggablePanel.disableBottomView(true);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
@Override
public void onClosedToRight() {
draggablePanel.removeAllViews();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
@Override
public void onUnMaximized() {
draggablePanel.disableBottomView(false);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
@Override
public void onMaximized() {
isFullScFromMini = true;
pipHandler.removeCallbacks(runnapipa);
pipHandler.postDelayed(runnapipa, PIP_DELAY);
draggablePanel.disableBottomView(false);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
}
});
TypedValue typedValue = new TypedValue();
getResources().getValue(R.dimen.x_scale_factor, typedValue, true);
float xScaleFactor = typedValue.getFloat();
typedValue = new TypedValue();
getResources().getValue(R.dimen.y_scale_factor, typedValue, true);
float yScaleFactor = typedValue.getFloat();
draggablePanel.setXScaleFactor(xScaleFactor);
draggablePanel.setYScaleFactor(yScaleFactor);
draggablePanel.setTopViewHeight(
getResources().getDimensionPixelSize(R.dimen.top_fragment_height));
draggablePanel.setTopFragmentMarginRight(
getResources().getDimensionPixelSize(R.dimen.top_frag_right_margin));
draggablePanel.setTopFragmentMarginBottom(
getResources().getDimensionPixelSize(R.dimen.top_fragment_margin));
draggablePanel.initializeView();
draggablePanel.maximize();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
}
但是,我在文件中得到的结果是
for(int i=0;i<listA.size();i++) {
writer.println(listA.get(i));
}
我期望:
Name_DS_1,LineString_1,UFI_1,Name_DS_2,LineString_2,UFI_2
Name1,Line1,Ufi1,Name2,Line2,Ufi2
我的整个代码是:
Name_DS_1,LineString_1,UFI_1,Name_DS_2,LineString_2,UFI_2
Name1,Line1,Ufi1,Name2,Line2,Ufi2
Name1,Line1,Ufi1,Name2,Line2,Ufi2
Name1,Line1,Ufi1,Name2,Line2,Ufi2
答案 0 :(得分:1)
这可能是解决您问题的方法,在这里您可以找到解决方法
JFrame frame = new JFrame();
ImageIcon icon = new ImageIcon(imgPath);
JLabel label = new JLabel(icon);
frame.add(label);
frame.setTitle(imgPath.substring(imgPath.lastIndexOf("\\")+1));
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
frame.setResizable(false);
//function to select area by mouse click
addHandlerToDraw(label,frame,icon);
一些提示:
private void addHandlerToDraw(JLabel label, JFrame frame, ImageIcon img) {
label.addMouseListener(new MouseAdapter() {
Point start = new Point();
Rectangle captureRect;
int k=0;
@Override
public void mouseClicked(java.awt.event.MouseEvent e) {
if(k==0) {
start = e.getPoint();
k=1;
} else {
k = 0;
Point end = e.getPoint();
captureRect = new Rectangle(start, new Dimension(end.x-start.x, end.y-start.y));
}
repaint(captureRect, frame, img, label);
label.repaint();
}
});
中声明的private void repaint(Rectangle rect, JFrame frame, ImageIcon img, JLabel label) {
BufferedImage bi = new BufferedImage(
img.getIconWidth(),
img.getIconHeight(),
BufferedImage.TYPE_INT_RGB);
Graphics2D g1 = bi.createGraphics();
g1.setColor(new Color(200,30,30,45));
if(rect != null) {
g1.drawRect(rect.x, rect.y, rect.width, rect.height);
frame.remove(label);
frame.add(new JLabel(new ImageIcon(bi)));
frame.setVisible(true);
}}
public class App {
public static List<String> listA = new ArrayList<>();
public static void main(String[] args) {
matchSplits();
}
public static void matchSplits() {
String header = "Name_DS_1" + "," + "LineString_1" + "," + "UFI_1" + "," + "Name_DS_2" + "," + "LineString_2" + "," + "UFI_2";
String fileName = "foo.csv"; // your file name here
File file = new File(fileName);
try (PrintWriter writer = new PrintWriter(file)) {
// no need to check if k == 0, this is outside the loop
listA.add(header);
// prepare your data here
for (int k = 0; k < 3; k++) {
String Name_DS_1 = "Name1";
String LineString_1 = "Line1";
String UFI_1 = "Ufi1";
String Name_DS_2 = "Name2";
String LineString_2 = "Line2";
String UFI_2 = "Ufi2";
String line = Name_DS_1 + "," + LineString_1 + "," + UFI_1 + "," + Name_DS_2 + "," + LineString_2 + "," + UFI_2;
listA.add(line);
}
// write your lines here
for (String s: listA) {
writer.println(s);
}
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
}
,所以不需要PrintWriter
try(...)
就足够了(至少在此代码段中如此)答案 1 :(得分:0)
之所以会这样,是因为您在for循环范围内编写了writer.close()。 为(int k = 0; k <3; k ++)
尝试删除writer.close()语句。 由于该语句仅在k = 0时才关闭写入过程。
因为PrintWriter实例是在try-with-resource语句中声明的,所以无论try语句是正常完成还是突然完成(如果这些资源实现AutoCloseable接口),它将关闭。
尝试执行此操作,希望它将解决您的问题:)